0

I read many threads about this subject, but I couldn't find anything that fits my needs.

I have an application with 3 activities (I'll call them screens here, more intuitive): A (MainActivity), B and C.
Screen A starts another process, retrieves some information from that process and sets the layout according to the answer. Then it gives you the opportunity to do something (write a text). This is done on OnCreate().

Screen B is not very interesting (presents the text and allows you to edit it).

Screen C has a single button which exits the application (basically starts the home activity). C also closes the process we started on A (after communicating some info to it).

So far so good. The problem is that when I restart my app, I see screen C again. This is not good for me, because I want the user to be able to edit the text, without having to go back to the first or second screen (not because it's so difficult, but because I need him to see the new info presented on screen A before he does anything. And this can't be done after C, because after C he's done, unless he decides to start the app again).
This means that when I get to screen C and press the button, my app is completed and there's nothing left for me to do there (but start over fresh). But if my app was suspended on screen B, there's no need to start fresh - I can just go back to it and continue from there (regular Android behavior).

So I set android:clearTaskOnLaunch="true"but now I have two problems:
1. It now starts fresh again (sort of, see #2) even if the app was suspended on B, which is not what I need.
2. It's not really starting fresh.... Yes, I see screen A. But all the things I did on OnCreate() are not done again, which means my other process is not up and running, and then my app crashes because I have no one to talk to.....

Is there any solution to such cases? I need to start fresh (but really fresh, with all the things I did on OnCreate() done again) only after previous run of the application was completed (we pressed the single button on screen C and got back to the home screen). In any other case of suspension (in A, B, and even C if we didn't press the button yet) I want the normal Android behavior (i.e. go back to the same screen).

Any help will be much appreciated.

Augustina
  • 259
  • 1
  • 4
  • 15

2 Answers2

0

To solve this you can take advantage of the Android Activity Lifecycle: http://developer.android.com/training/basics/activity-lifecycle/stopping.html

Simply call finish() in Activity B and C in onStop() or onPause() appropriately to wipe away those activities (or "screens" as you say) when the app is closed. Activity A should reopen holding the last state when you open your app back up.

If you're looking to wipe out the last state in Activity A as well, then add a finish() call to it as well where appropriate (probably onStop or onPause again per your use case)

Good luck!

Rob S.
  • 3,599
  • 6
  • 30
  • 39
  • right, screens = activities :) What do you mean by saying "Activity A should reopen holding the last state when you open your app back up"? I don't need the last state, I need to start fresh in case I completed my app (i.e. I pressed the button on activity C) – Augustina Aug 17 '14 at 17:04
  • Oh, gotcha, I didn't read carefully enough. In that case, just call finish() in onStop or onPause appropriately in Activity A as well. I'll add this to my answer.. – Rob S. Aug 17 '14 at 17:09
0

I had a similar app before, a survey app, and had the same situation. A->B->C and once you finish C you go straight to A and can't go back but you can go back from B->A. Here what I did:

1) Add this to A Activity when you start B Activity:

        Intent start;
    start = new Intent(MainActivity.this, RecordName.class);
    startActivityForResult(start, 2);

and this method in your A activity:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == 2) {
         if(resultCode == RESULT_OK){      
             //String result=data.getStringExtra("result");
             finish();
         }
      }

2) In B Activity add this when you start C Activity: Intent start; start = new Intent(B.this, C.class); startActivityForResult(start,1);

and this again in your B activity:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == 1) {
         if(resultCode == RESULT_OK){      
             //String result=data.getStringExtra("result");
             finish();
         }
      }

3) In your C activity add this to your press method:

        Intent start;
        start = new Intent(C.this,A.class);
        // pass any data back to A activity 
        start.putExtra("Key", String);
        startActivity(start);
        // to close previous activities 
        Intent returnIntent = new Intent();
        //returnIntent.putExtra("result","finish");
        setResult(RESULT_OK,returnIntent);     
        // close current activity
        finish();

I hope this helps

Karim
  • 766
  • 7
  • 9