I have a main_activity with 3 different Fragment and a SlideActivity
. I could make transactions among 3 fragments successfully in my MainActivity
.
Now I add 1 button in the MainActivity
and it will open the SlideActivity
. On the SlideActivity
, there are 3 buttons in the navigation bar to switch to each different fragments which I already created.
The problem is when I click the list button in the navigation, an error comes out shows that
No view found for id 0x7f080052 (com.example.learnfragment:id/fragment_container) for fragment fragment_main{e1acaf7 #0 id=0x7f080052}
It seems that it cannot find the FrameLayout
ID by R.id.
and I believe because the navigation buttons are in the SlideActivity
and it cannot find the ID which is in the MainActivity
.
But how should I do for switching back to the MainActivity
as well as changing different fragment?
Here's the navigation_slide_activity.xml
<include
layout="@layout/app_bar_slide"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_slide"
app:menu="@menu/activity_slide_drawer" />
The Navigation_Slide_Activity.java
:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Navigate back to the Main Fragment
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, new fragment_main());
ft.commit();
}
}
And the main_activity.xml
:
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="392dp"
android:layout_height="496dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" />
Finally, the MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create Fragment
if(savedInstanceState == null){
getSupportFragmentManager().beginTransaction().add(
R.id.fragment_container,
new fragment_main()).commit();
}
}
Get a public static
variable in your MainActivity
.
public static int FRAGMENT_TO_BE_LOADED = 0;
Now from the SlideActivity
, set the variable to a number (e.g. 3, that is you want to move to the third fragment when you are returning to your MainActivity
.
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
MainActivity.FRAGMENT_TO_BE_LOADED = 3;
finish();
}
}
Now in your MainActivity
, you need to have an onResume
function which will check the variable and load the fragment accordingly.
@Override
protected void onResume() {
super.onResume();
if(FRAGMENT_TO_BE_LOADED == 1) loadFragment1();
else if(FRAGMENT_TO_BE_LOADED == 2) loadFragment2();
else if(FRAGMENT_TO_BE_LOADED == 3) loadFragment3();
}
Hope that solves your problem.