Go back to a specific activity from the stack.

I am trying to do something like a file manager. And in the action bar I want to make folder navigation, for example, in the Google Drive application. I need to create a method that can go to the previous activity by number from the end or something like this.

Example:

So, if I have a stack: [1] β†’ [2] β†’ [3] β†’ [4] β†’ [5]

And I need to go to the second: so I need to remove [3], [4] and [5] from the stack and go to [2].

All actions are one class ContentActivity.java.

How can I do that?

UPDATE:

Some code as I run the actions:

public class ContentActivity extends Activity implements AdapterView.OnItemClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); Intent intent = getIntent(); String folderToOpen = intent.getStringExtra("folderName"); fillList(folderToOpen); } @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { ... Intent intent = new Intent(ContentList.this, ContentList.class); intent.putExtra("folderName", item.getName()); startActivity(intent); } } 
+9
source share
4 answers

Assuming Activity2 is the second action you want to use,

Try the following:

 Intent intent = new Intent(this,Activity2.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

According to Android Documentation about FLAG_ACTIVITY_CLEAR_TOP

If it is established and the launched activity is already running in the current task, then instead of launching a new instance of this activity, all other activities on top of it will be closed and this intention will be transferred (now from above) to the old activity as a new intention.

+24
source

Skip Manifest Attribute Activity

It depends on the need, but if we just want to just skip the activity in the return flow, then it is useful to remove this activity from the history in the manifest.

[1] β†’ [2] β†’ [3] - normal flow

[1] <- [3] - reverse flow

Then for [2] . We can set the noHistory attribute in the Manifest attribute:

 <activity android:name=".SecondActivity" android:noHistory="true" /> 

Thanks to this approach, our [2] activity will never be launched in the return flow.


Use intent flag

Removing activity from the history stack is not always a good idea, for example, if our activity sometimes requires a reverse flow, and sometimes not, then to start the drawing we need to set a flag with the intention:

 Intent intent = new Intent(this, FirstActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

And it’s very important - in the FirstActivity manifest to set the launch mode to singleTop .

 <activity android:name=".FirstActivity" android:launchMode="singleTop" /> 

Without activating the activity, the launchMode attribute will be restored.

+6
source

use this

 Intent intent = new Intent(this,Activity2.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 
0
source

For KOTLIN

  val intent = Intent(this, yourDestinationActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) startActivity(intent) 
0
source

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


All Articles