Search code examples
javaandroidandroid-activityfragment

Acivity1 Fragment2 -> Activity2 (after button click) -> Activity1 Fragment2 navigate from activity to specific fragment


Hello I have an application that implements bottom navigation component.

There is a main activity with 2 fragments (home fragment, note fragment).

I am going from note fragment to another activity called AddNoteActivity.

After I click a button in the AddNoteActivity I want the screen to show the main activity with the note fragment.

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

I understand that this code will launch the main activity but it automatically loads the home fragment because of its set as the app:startDestination in moblie_navigation.xml.

How would I get the main activity to load up the note fragment upon button click in the AddNoteActivity?

Also I have an action bar with up on navigation enabled. When I click the up arrow, I go back to the note fragment like its supposed to.


Solution

  • For go to the fragment just write

    on your button click {

    finish();

    }