简体   繁体   中英

When an activity with “singleTask” launchMode comes to Foreground - Android

I have an activity with "singleTask" mode. when this activity goes to background by taping on home key or other application, which methods will be called if it backs to foreground?

onCreate(Bundle savedInstanceState) with savedInstanceState != null
onRestoreInstanceState(Bundle savedInstanceState)

I think because there is one instance of this activity, it doesn't need to save and restore its state and it retain its own state always. am I right?

The sequence of methods that will be invoked is as follows,( in case when Android has killed the process hosting the Activity while the application was in the background, ) when Activity with singleTask mode comes in foreground:

1.onCreate
2.onStart
3.onRestoreInstanceState and
4.onResume

Below is sample code to demonstrate the concept: Activity Declaration in AndroidManifest.xml: <activity android:name="Second" android:launchMode="singleTask"></activity>

public class Second extends Activity {

    EditText mEdit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imageview_not);     
        if (savedInstanceState!=null){
            Log.e("onCreate of Actiity", savedInstanceState.getString("editval"));      }
        mEdit=(EditText) findViewById(R.id.editText1);

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.e("Second", "onResume");
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.e("Second", "onStart");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);        
        outState.putString("editval", mEdit.getText().toString());
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        Log.e("Second", "onRestoreInstanceState");
        if (savedInstanceState!=null){
            Log.e("onRestoreInstanceState", savedInstanceState.getString("editval"));
        }
    }


}

For illustration purpose, I am just using Edit Text and Android saves its state during configuration change, or when user press HOME etc.

Note that onSaveInstanceState(Bundle outState) is called before an activity may be killed so that when it comes back some time in the future it can restore its state using onRestoreInstanceState(Bundle savedInstanceState) .

Also as mentioned by David in another Answer,If Android has not killed the process hosting the Activity, then bringing the task from the background to the foreground will not cause either onCreate() nor onRestoreInstanceState() to be called.

If Android has not killed the process hosting the Activity , then bringing the task from the background to the foreground will not cause either onCreate() nor onRestoreInstanceState() to be called. The Activity doesn't need to be created (it already exists) and the stage doesn't need to be restored, because it hasn't been changed.

If, however, Android has killed the process hosting the Activity while the application was in the background, when the user returns to the application Android will create a new process for the application, create a new instance of the Activity , call onCreate() passing the saved instance Bundle as a parameter. It will also call onRestoreInstanceState() passing the saved instance Bundle as a parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM