In my app I am using Fragments in one Activity, and I am trying to replace them. This works fine, but on back press I want to switch to previous fragment. I found addToBackStack() method but it is not working. Here is the code in MainActivity:
MainActivity.java:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment1 fragment1 = new Fragment1();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
public void changeFragment(Fragment f)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content, f);
fragmentTransaction.commit();
}
@Override
public void onBackPressed()
{
if (getSupportFragmentManager().getBackStackEntryCount() > 0 )
{
getSupportFragmentManager().popBackStackImmediate();
}
else
{
super.onBackPressed();
}
}
}
Fragment1.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Fragment1 extends Fragment
{
private Button buttonFragment1;
public Fragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
buttonFragment1 = (Button)getActivity().findViewById(R.id.buttonFragment1);
buttonFragment1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Fragment2 fragment2 = new Fragment2();
((MainActivity) getActivity()).changeFragment(fragment2);
}
});
}
}
Fragment2.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
public Fragment2()
{
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment2, container, false);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asteam.myapplication.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fraggment" />
</RelativeLayout>
fragment_fragment1.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.asteam.myapplication.Fragment1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonFragment1"
android:text="Send to fragment2"/>
</RelativeLayout>
</FrameLayout>
fragment_fragment2.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.asteam.myapplication.Fragment2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment2" />
</RelativeLayout>
</FrameLayout>
and manifest xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asteam.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I tried to override the onBackPressed() method:
@Override
public void onBackPressed()
{
if (getSupportFragmentManager().getBackStackEntryCount() > 0 )
{
getSupportFragmentManager().popBackStackImmediate();
}
else
{
super.onBackPressed();
}
}
but it is not working, if I show the stack number after I click the button I get 1. I tried changing from android.support.v4. to android. to java fragment files and main activity and changed MainActivity to extend from Activity but that didn't worked either. What am I doing wrong? My app supports API 14 and above. Any help would be appreciated.
Edited
Plus logcat:
02-08 14:38:49.191 10417-10417/? I/art: Not late-enabling -Xcheck:jni (already on)
02-08 14:38:49.266 10417-10417/com.example.asteam.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.asteam.myapplication-2/lib/x86
02-08 14:38:49.322 10417-10432/com.example.asteam.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-08 14:38:49.452 10417-10432/com.example.asteam.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
02-08 14:38:49.489 10417-10432/com.example.asteam.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented
02-08 14:38:49.489 10417-10432/com.example.asteam.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabebfe80, error=EGL_SUCCESS
02-08 14:38:55.915 10417-10432/com.example.asteam.myapplication E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab852fc0
fragmentTransaction.addToBackStack(null);inchangeFragmentmethod.