简体   繁体   English

Android 保存实例状态

[英]Android SaveInstanceState

I have two activities, say A and B. Activity A has a Text View, two buttons B1 to increment the counter and B2 to navigate to activity B. I have entered some text into Text View and incremented the counter value.我有两个活动,比如 A 和 B。活动 A 有一个文本视图,两个按钮 B1 增加计数器和 B2 导航到活动 B。我在文本视图中输入了一些文本并增加了计数器值。 I navigate to activity B by clicking button B2.我通过单击按钮 B2 导航到活动 B。 Activity B2 has Up navigation that returns to activity A. I need to retain the text view and counter value when i return to activity A from B. What is the best way to do it...???活动 B2 具有返回到活动 A 的向上导航。当我从 B 返回活动 A 时,我需要保留文本视图和计数器值。最好的方法是什么......? I have tried SavedInstanceState.我试过 SavedInstanceState。 But OnrestoreInstanceState is not called when i return to activity A from B. I have tried Shared Preferences also.但是当我从 B 返回到活动 A 时,不会调用 OnrestoreInstanceState。我也尝试过共享首选项。 But it i think its not the best way..andro但我认为这不是最好的方法..andro

Add this to your activity B in your Manifest:将此添加到您的清单中的活动 B 中:

    <activity android:name="com.example.android.ActivityB" android:label="@string/title_activity_view_news" android:parentActivityName="com.example.android.ActivityA"> 
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.android.ActivityA" /> 
</activity>

And this to your activity A in manifest:这对于清单中的活动 A:

<activity android:name="com.example.android.ActivityA" android:label="@string/app_name" android:launchMode="singleTop"> //**HERE**//  
</activity>

Change values to match your packages and activity names... So you have to add android:launchMode="singleTop" in your <activity> A section.更改值以匹配您的包和活动名称...因此您必须在<activity> A 部分中添加android:launchMode="singleTop"

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

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