0

I have 2 activity: home activity and medicine detail activity. medicine detail activity is a child of home activity

AndroidManifest

<activity android:name=".MedicineDetailActivity"
    android:label="Medicine Detail"
    android:parentActivityName=".HomeActivity" />
<activity
    android:name=".HomeActivity"
    android:label="HOME PAGE"
    android:theme="@style/AppTheme.NoActionBar" />

Home Activity has a drawer, one of the menu is called medicine list, which opens medicine list fragment. Inside medicine list fragment is a listview which shows a list of medicines. If medicine is clicked, it will open Medicine Detail Activity.

So basically, the order that I want is like this:

  1. In the home activity, click menu to open the drawer
  2. In the drawer, click menu called "Medicine List", it will open medicine list fragment containing a list of medicine.
  3. Click one of the medicine, and it will open Medicine Detail Activity, showing information of medicine selected
  4. Click back arrow button, and it (should) return to home activity with medicine list fragment opened and selected.

I'm stuck until step 4 when I click the arrow button, it returns to Home Activity with home fragment opened as it is a default. I tried these but still not working:

MedicineDetailActivity

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
    }
    return false;
}

@Override
public void onBackPressed() {
    super.onBackPressed();

    Intent data = new Intent();
    data.putExtra("fragment", "med_list_fragment");
    setResult(RESULT_OK, data);
    finish();

}

HomeActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    Intent intent = getIntent();
    fragmentName = intent.getExtras().getString("fragment");

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    if (savedInstanceState == null{
        if (fragmentName != null) {
            //open fragment by fragmentName
        }
        else {    

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();

            navigationView.setCheckedItem(R.id.nav_home);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.e("Is it in?", "In here");
}

When back arrow button is clicked I intend to pass data called fragment_name, so when I get it in Home Activity it will open medicine list fragment instead of the default home fragment. Unfortunately, This line where I want to get the data will return error:

fragmentName = intent.getExtras().getString("fragment");

Alternatively, I tried to use onActivityResult that is said to be executed when an activity is opened (in this case, when Home Activity is reopened after Medicine Detail Activity is closed). But it was never executed at all:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.e("Is it in?", "In here");
}

How can I get parent activity to open a certain fragment by default after child activity is closed? Please help.

  • What are you trying to achieve by setting result and finishing the activity in onBackPressed? Also show us the code how you're navigating to the other activity. – Isak Sep 20 at 8:37
  • @Isak I edit it, hopefully it is clearer – Henry Gunawan Sep 20 at 9:34
0

The two activities are distinct. You could achieve this by using startActivityForResult() to receive the onActivityResult() callback. But you should revise your point 3 and 4:

  1. Click one of the medicine, and it will open Medicine Detail Activity, showing information of medicine selected.
  2. Click back arrow button, and it just goes back to the home activity.

Basically setup your app to not interfere with the task- and back-stack. If you leave an activity without calling finish() or any other fragment transaction, the activity will manage this state and keep it as is.

If the state is not kept, you're not doing not enough, but rather too much you should not do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.