Switch between actions in android?

In my Android app, I have the following requirement.

Activity A → Activity B (Go to option A) → Activity C (option "Go to option", "Go to B")

1) To go to activity A from Activity B, I used the onBackPressed() method.

2) To go to step B from Activity C, I used the onBackPressed() method onBackPressed() .

everything is working fine.

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

How can i do this?

Edited 1:

Activity A is my main activity. I do not want to restart activity using Intent.i to resume action A from action c. (as, for example, I made from operation B using onBackPressed).

Edited 2 (with the answer):

Ok guys. Thank you all for helping me with 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 using this link that solved my problem. Thanks to everyone, and I really appreciate that.

+3
source share
7 answers

I assume that you do not want to use Intent, because whenever you use Intent to go to an activity, pressing the Back key will move to the previous action (activity C). In this case, I suggest that you enable the FLAG_ACTIVITY_CLEAR_TOP flag. It will destroy all previous actions and allow you to go 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 action A without removing any actions.

See more details.

+5
source

There are two ways to achieve this: By launching an intent using Intent Filter Clear the top stack, but according to your question, you are not interested in this method. The second method is to launch actions using StartActivityForResult.

Using intent flags:

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

The second way using startActivityForResult:

In action 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 action C

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

A reliable solution ...

Getting started using StartActivityForResult()

And based on the conditions that you set for ResultCodes in the actions .. Something like this ..

GO_TO_ACT_A = 1; GO_TO_ACT_B = 2;

And in all onActivityResultMethod assets 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 } 
+3
source

If you want to recreate your activity, 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 recreated, but you will have to override onNewIntent() , which will be called in your activity A.

UPDATE:

This used Intent , but since your requirement was not used by Intent , so in this case the best approach would be to use startActivityForResult , as there are already a few answers above. I do not develop it.

+1
source

you can pass the context of Activity B to action C, and then in an Activity C call

 ((Activity) bContext).finish(); 

which closes Activity B and then you can call

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

which causes Activity C to close and return to Activity A, since Activity B is already closed.

0
source

Why can't you use

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

"... This method is called before the action can be killed, so that when it returns some time in the future, it can restore its state. For example, if activity B is started before activity A and some point activity A is killed to restore resources, activity A will be able to save the current state of its user interface using this method, so that when the user returns to activity A, the state of the user interface can be restored via o nCreate (Bundle) or onRestoreInstanceState (Bundle) ... "

I think this is what you wanted. With this, you can use the Intent from Activity C. Yes, then it goes to the onCreate method, but there you check to see if there is any stored instance. If there is any saved instance, you do not need to load the rest of the onCreate elements. This will speed up your application.

0
source

You can do it like:

Back option:

  • 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); 

    }

Button Usage:

  • 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
source

Source: https://habr.com/ru/post/1482927/


All Articles