Activity Maps restarts whenever I minimize

0

Well, on android 4.2.1 is working, but on another device with android 4.2.2 it restarts, ie it does not save the state of the activity, this problem only appears with the activitys that I use map, in the other it works quietly . Below is the start of the activity header.

   public class Map_Activity extends FragmentActivity implements
            GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, OnMapReadyCallback
            , com.google.android.gms.location.LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {


        @Override
        protected void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            setContentView(R.layout.activity_map_);
    
asked by anonymous 12.07.2016 / 01:58

1 answer

0

What happens is that when the application loses focus, it begins a cycle that is part of the life cycle of an Android application, when an Activity is no longer visible (nor in the foreground) the onStop is called this means that from this point on, the system (whose management has some changes from version to version of Android) may decide to destroy this activity (calling the onDestroy() method).

Android Life Cycle:

WhenthesystemdestroysyourActivity,itstorestheActivitystateintheBundles,soallViewsmusthaveID'stobeidentified.

However,Androidisnotalwaysright,anditrestoreseverythingasyouwish,soifActivityisimportanttomaintaincertainbehavior,ideallyyoushouldnotrelyonAndroidtodosoandensurethatinanyversion,yourviewwillmaintainthebehavior.

Then,youneedtooverridetheonSaveInstanceStateandonRestoreInstanceStatemethod,andtreatthedatasavedinonCreate

Example:

staticfinalStringSTATE_SCORE="playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}

Source Android Docs .

    
12.07.2016 / 16:35