2

I am new in android and I have total 6-7 activities in my application. I want to know how can I manage my activities properly means when I move to the A->B->C->D like that. Then how can I move that the stack of these activities not created.

On moving from one activity to the other I am using the below code:

Intent intent=new Intent(current.this,next.class);
                startActivityForResult(intent, 0);

And now if I want to move back on the earlier activity I used the code as:

Intent start = new Intent(current.this,next.class);
                    startActivity(start);
                    finishActivity(0);
Janusz
  • 187,060
  • 113
  • 301
  • 369
ADB
  • 567
  • 2
  • 5
  • 20

4 Answers4

3

Is there a special reason that you don't want to use the activity stack and let the activities handle themselves?

The Android system has done a very good job with the activity lifecycle. It allows you to start an Activity from different places without confusing the user because the back button will bring the user back to a different activity.

If you don't have a very good reason to not use the Android guideline try to stick to the way the system is doing it. Every other thing will only give you problems.

You are starting activities for a result but how I understand you you will never return to them.

You can start an Activity and after that just finish the current Activity. That way the activity will not be put on the back stack. Now you need to listen for back button pushes and create the activities that you want to bring the user to.

If you want to move from Activity A to D like going to the start/home screen of you app you do the following:

  Intent goBackToA = new Intent(context, StdActivity.class);
  goBackToA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  startActivity(goBackToA);

The flag FLAG_ACTIVITY_CLEAR_TOP will tell the system that if the backstack contains an instance of the Activity this activity will be shown and all activity that are between the current activity and the target activity are removed from the backstack. This allows you to go back to a home activity without creating huge loops that the user can move through with the back button.

Janusz
  • 187,060
  • 113
  • 301
  • 369
  • in one case I have to move from activity D->A then in that case what should I do. Also please tell me the standard way to move from one activity to another. I want to finish this i am facing one prob in my application. http://stackoverflow.com/questions/10585310/the-application-still-in-ram-after-using-it-and-take-more-2-space-each-time-i-pl see that link – ADB May 15 '12 at 06:18
1

To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapear.

  • in one case I have to move from activity D->A then in that case what should I do. Also please tell me the standard way to move from one activity to another. I will really thankful to u – ADB May 15 '12 at 06:08
1

To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapeear or you can press Back button to see the previous Activity .

ebbicsku
  • 51
  • 1
  • 1
  • 7
1

whenever you want to navigate from one class to another use this code, may be this help you to navigate the Activity,

 Intent nextpage = new Intent(CurrentActivity.this,NextActivity.class);
    startActivity(nextpage);
    this.finish();
user1208720
  • 515
  • 2
  • 6