简体   繁体   中英

Go back to fragment of Activity A from Activity B

In my app, am using a navigation drawer (in Fragment A) to navigate to fragments:

  public void displayView(int viewId){
    Fragment fragment = null;
    String title = getString(R.string.app_name);
      switch (viewId) {
      case R.id.nav_menu:
            fragment = new MenuFragment();
            title = getString(R.string.menu_title);
            viewIsAtHome = false;
            break;

        case R.id.nav_reservation:
            fragment = new ReservationFragment();
            title = getString(R.string.reservation_title);
            viewIsAtHome = false;
            break;
         ...
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }

    // set the toolbar title
    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle(title);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
     } 
  }

displayView() is called in onNavigationItemSelected :

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    displayView(item.getItemId());
    return true;
}

Now, in ReservationFragment I am displaying a list of reservations and a FloatingActionButton to start Activity B where the user can add a reservation if there are no reservations. When the user is done adding a reservation, I want to display it in the Reservation fragment. This requires me to "go back" to the Fragment.How do I accomplish this since Activity B knows nothing about Activity A?

What I've tried:

I tried creating a method in Activity A like this:

public void navigateToFragment(int viewId) {
  displayView(R.id.nav_reservation);
}

and then called this method from Activity B:

saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           new MainActivity().navigateToFragment(R.id.nav_reservation);
           //MainActivity is Activity A
        }
    });

the app crashes due to a nullPointerException in displayView() from the line:

String title = getString(R.string.app_name);

This isn't surprising since am creating a new MainActivity object that knows nothing about the previous state of the Activity, right?

This question mirrors my problem but is based on Settings so I can't really use the answer in my case. How do I accomplish this task?

There are three quick methods that come to my mind. First you can start activityB with startActivityForResult and handle the result in activityA after user does what he wants in activityB. Second you can set activityA as singleTop and before finishing activityB you can startActivityA with clearTop an intent flag called clear_top( https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP ). Last but not the least, you can connect two activity by binding service in both activities and communicate via that service that you bound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM