Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigate Back to Activity; Don't Reload Parent

I have a scenario where I am clicking on a ListFragment and spinning up a new Activity like 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 fine and displays the new activity.

I've modified this activities manifest so it points back to its parent activity (in this case main activity)

However the problem I have is when the back button is pressed, it reloads the entire parent. The parent is a list and I don't want it to reload the users position. How can I prevent this?

As a note. The parent houses a Page Tab Strip.

I'm sure this is a relatively simple fix...

like image 376
aherrick Avatar asked Jul 09 '15 23:07

aherrick


2 Answers

What do you mean by "back button"? Is it the up button in the toolbar? If that's the case, edit the onOptionsItemSelected in your 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);
}

So when user press the Up button it will get the behavior of the back button in the navigation bar.

like image 128
Kise Avatar answered Sep 25 '22 03:09

Kise


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

like image 45
div Avatar answered Sep 25 '22 03:09

div