简体   繁体   中英

Save and Restore FragmentActivities in an ArrayList

I have a list containing Activity-A , each Activity-A has different values inside it. However they all have the same class, which is Activity-A . When the user navigates back to the list from an activity, I store that activity in an ArrayList and retrieve it and start it as an intent when the user clicks on the same activity in the list again. However, that causes it to be re-created. I want it to be resumed from the state it was stored in the ArrayList. How can I do this?

Edit: I've heard of the FragmentManager and how it easily stores and can retrieve fragments. However, is there something like FragmentManager for activities? Is ActivityManager like the FragmentManager?

Edit: To clarify my question a bit more, I'm trying to:

  • Save the FragmentActivity to an ArrayList when it gets stopped
  • Resume it from the state before it was stopped.

However, when I navigate to the FragmentActivity stored in the ArrayList, the FragmentActivity is recreated from scratch, instead of resuming from its previous state.

I understand that I have to do something with saving its state. However, I doubt I can save all of its inside contents; there's lots of fragments in it.

Here's the code:

listItem.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showActivity(id);
            }


private void showActivity(int id) {
        ActivityA activityA = StaticLab.get(getActivity())
                .getActivityA(id); // getActivityA simply returns the ActivityA from the ArrayList which contains the specified id
        if (activityA == null) {
            createNewActivityA()
        }
        Intent i = new Intent(getActivity(), activityA.getClass());
        startActivity(i);

    }

You need to crate a Static Arraylist object.

So every time you come on that activity you dont create a fresh object of ArrayList

You can use global variable but your application memory usage will be large. Where do you retrieve data? Web Service Or Client DB SQLite? If you retrieve data from SQLite, retrieve time so fast. If you retrieve from Web Service, you should save to SQLite. I don't know clear your question. just give the idea for you. Hope for Help.

Global Variable Usage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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