0

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);

    }
sm123
  • 31
  • 5

2 Answers2

0

You need to crate a Static Arraylist object.

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

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
0

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

Community
  • 1
  • 1
B M
  • 1,863
  • 1
  • 25
  • 40
  • Thanks, but I'm not quite sure I understand what you are saying. If you are referring to where the activities are stored, they are stored in an ArrayList. The things it contains cannot be put into an SQLite. – sm123 Sep 01 '14 at 10:22
  • you can try with global variable. In activity state you want to store array list? i think that can't be. you can read activity life cycle .http://developer.android.com/reference/android/app/Activity.html – B M Sep 01 '14 at 10:35
  • I'm not quite sure what you mean by using a global variable. The class which contains the ArrayList is static, so there aren't multiple copies of it. When I navigate to the ListView, the activity calls onStop(), but when i navigate back to it via an intent, it calls onCreate() all over again, instead of onStart(), which I think may be due to using Intent.startActivity(i) – sm123 Sep 01 '14 at 10:39
  • please try onResume() event. onResume() to restore data previous state. – B M Sep 01 '14 at 10:53
  • How do I do that? Sorry, I'm new to android programming and where I am right now, the internet is quite slow. – sm123 Sep 02 '14 at 12:01