2

I am navigating from Activity_A to Activity_B and showing a list view in Activity_B , when I press back button in Activity_B control comes to Activity_A. Now, again I navigate to Activity_B, now I dont want to create the list View once again instead I want to show the list View previously created. How to do this ? Any one please help me...

Thanks in advance....

Prabhu M
  • 3,534
  • 8
  • 48
  • 87

2 Answers2

1

when you start Activity_B set the Flag

FLAG_ACTIVITY_REORDER_TO_FRONT

Intent intent = new Intent(Activity_A.this, Activity_B.class);

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

startActivity(intent);

this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.

Sujit
  • 10,512
  • 9
  • 40
  • 45
  • Hi, if the activity is not running , how to do this. Because when I press back button the activity gets removed from stack . Am I right? – Prabhu M Jun 28 '11 at 11:13
  • Yup..thats right.. You can either do this. When you first time open your List and show data from the ArrayList. it will check the size of ArrayList is > 0 or not. If not then it can retrieve it from web service otherwise show the previous data. – Sujit Jun 28 '11 at 11:27
  • Ya, these things we can do it in different ways. But I want to know , is there any standard way from android to achieve this. Any way thanks for ur suggestion.... – Prabhu M Jun 28 '11 at 11:35
  • AFAIK there is not any standard way to do this. but yes you can use caching concept to achieve this. I haven't try it before.see this link http://stackoverflow.com/questions/1945201/android-image-cache – Sujit Jun 28 '11 at 11:41
  • @ sujit....My code is: public void onClick(View v) { Intent intent = new Intent(activityA.this, activityB.class); intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent);}...activityB has a listView scrolled down to some 100th item....when i am in activityA and if i click a button to go to activityB it shows me the 100th item which is very fine...but then i go to activityA using device back button and now when i try to go to activityB using this onClick code shown, it recreates the listView....how can i make this show the 100 th item which was shown previously – Housefly May 24 '12 at 08:54
  • When you press back button your Activity gets popped from the the stack. So these is now way to get the this Activity again instead of starting it again. You can do one thing, make your ArrayList that hold the data for listview as static and fill it when you first time enter to Activity B and for the second times onwards you can fetch the this static ArrayList. – Sujit May 25 '12 at 06:01
1

You need to save your Activity's state.

http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

tdtje
  • 190
  • 1
  • 8