8

In my android application, I have following requirement.

Activity A --> Activity B(Go to A Option) --> Activity C(Go To A Option,Go To B Option)

1) To Go To Activity A from Activity B i have used onBackPressed() method.

2) To Go To Activity B from Activity C i have used onBackPressed()method again.

those are working fine.

3) Now i want to go to Activity A from Activity C (without Intent calling).

How can i do this?

Edited 1:

Activity A is my Main activity i don't want restart the activity by using Intent.i want to resume Activity A from activity c.(like i did from activity B by using onBackPressed).

Edited 2(With Answer):

Ok guys. Thanks everyone for giving me your help on my question.finally i found a simple answer similar to @Paresh Mayani's answer.

Answer:

 Intent a = new Intent(getApplicationContext(),ActivityA.class); a.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(a); 

i got this nice solution by using this link that solved my problem. thanks to everyone again and i really appreciate that.

2
  • 2
    What is your problem with using intent?? Commented Mar 30, 2012 at 4:50
  • 1
    Activity A is my Main activity i don't want restart the activity by using Intent.i want to resume Activity A from activity c.(like i did from activity B by using onBackPressed) clear?? Commented Mar 30, 2012 at 4:55

7 Answers 7

7

I assume that you don't want to use Intent because whenever you use Intent for moving to activity A pressing Back key will move to the previous activity (activity C). In this case I would suggest you to include FLAG_ACTIVITY_CLEAR_TOP flag. It will destroy all the previous activity and let you to move to Activity A.

 Intent a = new Intent(this,A.class); a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(a); 

Alternatively, you can try the FLAG_ACTIVITY_REORDER_TO_FRONT flag instead, which will move to activity A without clearing any activity.

For more, check this.

Sign up to request clarification or add additional context in comments.

4 Comments

i guess somebody didn't like it, people doesn't appreciate other people putting in their time to help.
Those who are downvoting, pls write an actual Reason!!
@sandy aah I was talking about my answer.
@PareshMayani this won't work as the user don't want to re-create Activity and FLAG_ACTIVITY_CLEAR_TOP will do that only re-create it.
5

There are two ways you can achieve this: By Starting Intent with applying Intent Filter Clear top stack, but according to your question you are not interested in this method. Second method is by Starting activities by StartActivityForResult.

By using intent flags:

Intent intent = new Intent(this, A.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

Second Way, by Using startActivityForResult:

In Activity B:

 Intent intent=new Intent(B.this, C.class); intent.startActivityForResult(intent); 

onActivityResult method:

 protected void onActivityResult(int reqCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK ) { String res = data.getExtras().getString("result"); if (res.equals("A")) { String msg = "RESULT: " + res; Toast.makeText(Login2.this, msg, Toast.LENGTH_SHORT).show(); finish(); } } } 

In Activity C

 Intent intent = new Intent(); intent.putString("result", "Hello, World"); setResult(RESULT_OK, intent); 

2 Comments

Thanks finally someone is here, which put some attention on my answer
yes why not, but using FLAG_ACTIVITY_CLEAR_TOP won't work as user don't want Activity A to get re-created and using FLAG_ACTIVITY_CLEAR_TOP it will get re-created. Rather startActivityForResult is a good option.
3

A reliable solution is ..

You start Activities Using StartActivityForResult()

And based on conditions You set ResultCodes in Activities.. Something like this..

GO_TO_ACT_A=1; GO_TO_ACT_B=2;

And in all Activies onActivityResultMethod check the result code..

if(resultCode==GO_TO_ACT_A){ finish(); //Assuming curently you are in Activity C and wanna go to Activity A } 

2 Comments

hey is my answer does not address the same solution?
@jitendrasharma .. may be.. but i really did't see that.. And the same answer I ve given a long time ago.. I don't remember which was that.. Thats why i told him to google it..
1

If you want to re-create your Activity then you can use FLAG_ACTIVITY_SINGLE_TOP

Intent intent = new Intent(this, A.class); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent); 

By doing this your Activity A will not be re-created rather you will have to override onNewIntent() which will be called in your Activity A.

UPDATE:

This was using Intent but as your requirement was without using Intent, so in that case better approach would be using startActivityForResult as already there are couple of answers above I am not elaborating it.

1 Comment

@Mayank ah!! really its a copy paste?
0

you can passon the context of Activity B to Activity C and then in Activity C call

((Activity) bContext).finish(); 

that'll close Activity B and then you can call

((Activity)getContext()).finish(); 

that'll cause Activity C to close and go back to Activity A as Activity B is already closed.

4 Comments

What if the The previous Activitys have been finished by OS... Exception..?.. Just out of curiosity..
@sandy good point, If the previous Activity is closed by OS then context will be NULL, so as you said there gonna be an exception, NPE.
i mean about the exception.if that is the case is there any way to awoid this?
yes catch the exception and move to next step, which is calling finish on yourself.
0

Why can't you use,

@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //Save your current instance } 

"...This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle)..."

I think that is what you wanted. With this you can use Intent from Activity C. Yes then it goes to onCreate method, BUT there you are checking whether there is any saved instance. If there are any saved instance you no need to load the rest of the onCreate items. It will speed your application.

Comments

0

You can do this as:

On back press option:

  1. onBackPressed(){ @Override public void onBackPressed() {

    // TODO Auto-generated method stub super.onBackPressed(); //Your method intent Intent intent = new Intent(A.this, C.class); startActivity(intent); 

    }

Using a button:

  1. button.setOnClickListener( new View.OnClickListener() {

     public void onClick(View v) { Intent intent = new Intent(A.this, C.class); startActivity(intent); // finish(); // may use finish if do not want endless loop }}); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.