0

I have an application with several Activities in Android A,B,C,D. I have a question about the back stack.

A->B->C->D it is a normal sequence.

My question is : when i press the back button in Activity D, i have to go to the Activity B, not back to ActivityC. Since ActivityC have some imageView i want to save, i don't want to use noHistory to destroy the ActivityC. it is possible to do that

A1->B1->C1->D1

the back stack should be: D1->B1->A1 . C1 saved some imageView and if C1 launch again, the imageView in C1 will contain the same images. Is it possible just modify the code in ActivityC??

thank you

3 Answers3

0

You should call finish() in activity C after, starting of activity D

startActivity(intent);
finish()
Ufkoku
  • 2,384
  • 20
  • 44
  • He can't destroy activity C, since there's an image there he want to save – Yash Mar 22 '17 at 06:49
  • He can use something to load image separately from activity, or use Picasso for example, it will cache image on disk – Ufkoku Mar 22 '17 at 06:53
  • Have method to save the image? If the image can save, finish() is ok. – yeun hang yeung Mar 22 '17 at 06:53
  • If you loading image from server, and you just don't want to waste user traffic again, you can use Picasso lib, it will cache image in RAM and disk. So the next load will be fast. – Ufkoku Mar 22 '17 at 06:57
0

Handle onBackPressed() of each activity. So that after back pressing on D, it will redirect to B, then similarly to A.

Sandeep Kharat
  • 500
  • 1
  • 4
  • 12
  • Can i just modify the code in ActivityC? Something like hide ActivityC in backtack – yeun hang yeung Mar 22 '17 at 06:55
  • Make a call to B in the the onBackPressed() of D. So it will redirect to B rather than C. and when you again call to existing C, it will come up with previous state. Just handle onBackPressed() of each activity to call existing activities from the satck – Sandeep Kharat Mar 22 '17 at 06:58
  • I think you should add launch modes to your answer. – Ufkoku Mar 22 '17 at 07:00
  • This will helpful: http://stackoverflow.com/questions/4038479/android-go-back-to-previous-activity – Sandeep Kharat Mar 22 '17 at 07:01
  • Because i am writing a library. The activityC is a library activity. I just can modify codes in library activity.So that, i cannot make change in B and D – yeun hang yeung Mar 22 '17 at 07:03
0

Use onActivityResult() methods of an activity:-

Here is how it will work -

Use startActivityForResult() method while you are starting activity

While when you are going back
Activity D - before finishing write -

Intent i = getIntent();
setResult(RESULT_OK);
finish();

Your D is finished and c's onActivityResult() will be called - save your data and call finish() with above methodology, It will go to B's onActivityResult().

Paresh P.
  • 6,677
  • 1
  • 14
  • 26