简体   繁体   English

再次调用后保持活动状态

[英]maintain state of activity after calling it again

I have two Activities A1 and A2 , on firing some event i am calling A2 (through intent) from A1. 我有两个活动A1和A2,在触发某些事件时,我正在通过A1调用A2(通过意图)。 Now inside A2 i am firing some event and based on that i am calling A1 again and passing data through intent). 现在在A2内部,我引发了一些事件,并基于此事件再次调用A1并通过intent传递数据。

Now the problem is When A1 gets called from A2 with data , A1 activity load with itself with a new state but i want to maintain its old state when A1 was first loaded. 现在的问题是,当从A2用数据调用A1时,A1活动会以新状态自行加载,但是我想在首次加载A1时保持其旧状态。 indirectly i don't want to call onCreate. 间接地,我不想调用onCreate。

so far i have tried following code in A1 activity , its a static method in A1 which load itself 到目前为止,我已经尝试在A1活动中遵循以下代码,它是A1中的一种静态方法,该方法会自行加载

public static void show(Context context , int index)    
    {
          final Intent intent = new Intent();
          intent.setAction(MC_MY_ACTION);
          intent.putExtra("routeIndex", index);
          context.startActivity(intent);

    }

from A2 i am calling A1 as follows from onOptionsItemSelected and passing the selectedMenuIndex 从A2我正在从onOptionsItemSelected调用A1,并传递selectedMenuInde​​x

A1.show(this,selectedMenuIndex);     

If your activity has finish()'d, you're not going to avoid going through onCreate() again -- however there's no need to worry about that. 如果您的活动已经完成(),那么您就不必避免再次执行onCreate()了-但是,您不必为此担心。 Just let your onCreate() examine the contents of its intent extras, and if there are none then follow your default initialization but if there are extras, make them carry enough state data so you can be back in the previous state. 只需让您的onCreate()检查其意图附加的内容,如果没有,则遵循默认的初始化,但是如果有附加,则使它们携带足够的状态数据,以便您可以返回到先前的状态。

If you want to save the state of your activity and have it persist through multiple onCreate() calls, you can use the Bundle object. 如果要保存活动状态并通过多次onCreate()调用使活动状态保持不变,则可以使用Bundle对象。 You can save to the Activity's Bundle in onSaveInstanceState() and then load it back up in onCreate(). 您可以在onSaveInstanceState()中保存到活动的捆绑软件中,然后将其重新加载到onCreate()中。 Take a look at http://developer.android.com/reference/android/app/Activity.html and http://developer.android.com/reference/android/os/Bundle.html . 看看http://developer.android.com/reference/android/app/Activity.htmlhttp://developer.android.com/reference/android/os/Bundle.html

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

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