简体   繁体   English

以完整的先前活动数据开始活动

[英]Start activity with previous activities data intact

I'm trying to start a new activity using intent and on finishing the activity, I want my previous activity to retain its data. 我试图使用意图开始一个新的活动,并在完成该活动时,希望我以前的活动保留其数据。 I already have other activities passing some data into this main activity but on starting this new activity, when I go back, all that data disappears and gives me a null pointer exception. 我已经有其他活动将一些数据传递到该主活动中,但是在开始该新活动时,当我返回时,所有这些数据都消失了,并给了我一个空指针异常。

Is it possible to retain the data from the previous activity? 是否可以保留上一个活动中的数据? I searched about it and found I could make a singleton class but it's usage is still unclear to me. 我搜索了一下,发现我可以做一个单例课程,但是对我来说尚不清楚它的用法。

PS: The real problem is I'm trying to pass a String ArrayList from this activity to the main activity but on doing so, the previous data (being passed on from previous activities) on the main activity disappears. PS:真正的问题是我试图将String ArrayList从此活动传递到主活动,但这样做时,主活动上的先前数据(从先前活动传递过来的)消失了。

If usage is unclear then you can use SharedPreference for now. 如果不清楚用法,则可以立即使用SharedPreference。

Saved the values in the shared preferences on receiving Intent data. 在接收到Intent数据后,将这些值保存在共享首选项中。

Then when you navigate to next screen and comeback, in the onResume of your activity load the data saved in the shared preferences. 然后,当您导航到下一个屏幕并返回时,在活动的onResume中加载保存在共享首选项中的数据。

Check that link https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en 检查链接https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

I think "single top" activity will solve your problem. 我认为“单身”活动将解决您的问题。

Also you can send data from any another android activity or fragment to your single top activity through intents and handle it in main activity in that way: 您也可以通过意图将数据从任何其他android活动或片段发送到您的单个top活动,并通过以下方式在main活动中进行处理:

@Override
    protected void onNewIntent(Intent intent) {
        ... = intent.getStringArrayExtra(KEY)
        ...
}

Also you can check that answer related to single top activities https://stackoverflow.com/a/1715405/6193843 您也可以检查与单项顶级活动相关的答案https://stackoverflow.com/a/1715405/6193843

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

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