开发者

Can I save different-2 instance of an Activity in Android?

I want to know can I save two or more instance of an Activity and can use those instances as per my requirements.

For example I have two Activities say A & B.

I go from Activity A to B by checking some condition. if condition meets first instance of Activity B should be started else another one.

For example- In Activity B I have a form which has some EditTexts,some CheckBOxes & form has two Buttons Save & Cancel. If user presses Save Button so it goes to Activity A & send all form's data which user filled to Activity A. Activity A displays all data filled by user.

But when user again goes to Activity B so he should see all the fields which user previously saved.

After ag开发者_开发技巧ain coming to Activity B user modifies data or fills other fields and instead of saving he presses Cancel Button goes to Activity A without sending any data.

so Activity A shows only previously saved data not modified or extra ones.

But when I again goes to B so I should only see data which user saved first time.

I think I can do this by using SharedPrefernces. But I want to know can I save two or more instances of Activity B so on condition basis I can use required one.


You can't really save an instance of the Activity in the method you mention. You are correct in your assumption that SharedPreferences is one way you can accomplish your task.

In your example, I would have Activity B always pull its form data from the SharedPreferences. Also, when you hit the Save Button, it would cause the form data to be persisted to the SharedPreferences. Finally, in Activity A, you would show the data from the SharedPreferences whenever it was available.

If you want to know if Activity B was exited via Save or Cancel in Activity A, you should start Activity B with startActivityForResult. Then, in Activity B, call setResult in the onClick handler for your Save or Cancel button. Finally, Activity A will get this result in onActivityResult.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜