简体   繁体   English

当具有“singleTask”launchMode的活动来到Foreground - Android时

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

I have an activity with "singleTask" mode. 我有一个“singleTask”模式的活动。 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: 当具有singleTask模式的Activity进入前台时,将调用的方法序列如下( 当Android在应用程序处于后台杀死托管Activity的进程时)。

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

Below is sample code to demonstrate the concept: Activity Declaration in AndroidManifest.xml: <activity android:name="Second" android:launchMode="singleTask"></activity> 下面是演示概念的示例代码: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. 为了便于说明,我只是使用编辑文本,Android在配置更改期间或用户按HOME等时保存其状态。

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) . 请注意,在活动可能被onSaveInstanceState(Bundle outState)之前调用onSaveInstanceState(Bundle outState)以便在将来某个时间返回时,可以使用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. 另外正如David在另一个答案中所提到的,如果Android没有杀死托管Activity的进程,那么将任务从后台运行到前台不会导致调用onCreate()和onRestoreInstanceState()。

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. 如果Android没有杀死托管Activity的进程,那么将任务从后台带到前台将不会导致调用onCreate()onRestoreInstanceState() 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. Activity不需要创建(它已经存在)和阶段并不需要恢复,因为它并没有被改变。

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. 但是,如果Android在应用程序处于后台时杀死了托管Activity的进程,当用户返回应用程序时,Android将为应用程序创建一个新进程,创建一个新的Activity实例,调用onCreate()传递保存的实例Bundle作为参数。 It will also call onRestoreInstanceState() passing the saved instance Bundle as a parameter. 它还将调用onRestoreInstanceState() ,将保存的实例Bundle作为参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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