7

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.

Community
  • 1
  • 1
Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45
  • 2
    What is your problem with using intent?? – COD3BOY Mar 30 '12 at 04: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?? – Dinesh Anuruddha Mar 30 '12 at 04:55

7 Answers7

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.

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
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);
jahroy
  • 22,322
  • 9
  • 59
  • 108
jeet
  • 29,001
  • 6
  • 52
  • 53
  • Thanks finally someone is here, which put some attention on my answer – jeet Mar 30 '12 at 05:20
  • 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. – Lalit Poptani Mar 30 '12 at 05:24
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
}
ngesh
  • 13,398
  • 4
  • 44
  • 60
  • 1
    hey is my answer does not address the same solution? – jeet Mar 30 '12 at 05:12
  • @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.. – ngesh Mar 30 '12 at 05:18
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.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
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
    }});
    
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.

Mayank
  • 8,777
  • 4
  • 35
  • 60
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.

AnujAroshA
  • 4,623
  • 8
  • 56
  • 99