Android Navigate Back to Activity; Do not restart parent

I have a scenario where I click on a ListFragment and start a new activity, as shown below:

public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Intent intent = new Intent(getActivity(), VenueBeerActivity.class); Parcelable wrapped = Parcels.wrap(mAdapter.getItem(position)); intent.putExtra("venue", wrapped); startActivity(intent); } 

This works great and displays new activity.

I changed this manifest of activity, so it indicates its parent activity (in this case, the main activity)

However, the problem is when the back button is pressed, it reloads the entire parent. The parent is a list, and I do not want him to reload the user’s position. How can I prevent this?

As a note. The parent has a page with page separators.

I am sure this is a relatively simple fix ...

+3
source share
2 answers

What do you mean by the back button? Is this a toolbar up button? In this case, edit onOptionsItemSelected in VenueBeerActivity to:

 @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case android.R.id.home: onBackPressed(); return true; } return super.onOptionsItemSelected(item); } 

Therefore, when the user clicks the Up button, he gets the behavior of the Back button on the navigation bar.

+6
source

In parent activity, set the android:launchMode to singleTop . This will prevent the system from creating a new parent instance when the up button is pressed.

+1
source

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


All Articles