java - Android addToBackStack(null) not working, closing the app -
in app using fragments in 1 activity, , trying replace them. works fine, on press want switch previous fragment. found addtobackstack() method not working. here 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 layout 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 layout 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 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 override onbackpressed() method:
@override public void onbackpressed() { if (getsupportfragmentmanager().getbackstackentrycount() > 0 ) { getsupportfragmentmanager().popbackstackimmediate(); } else { super.onbackpressed(); } }
but not working, if show stack number after click button 1. tried changing android.support.v4. android. java fragment files , main activity , changed mainactivity extend activity didn't worked either. doing wrong? app supports api 14 , above. 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 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
change method this
public void changefragment(fragment f) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(android.r.id.content, f); fragmenttransaction.addtobackstack(null); fragmenttransaction.commit(); }
then should remove fragmenttransaction.addtobackstack(null);
oncreate
of mainactivity
.
also, in fragment1
code inside onactivitycreated
should moved oncreateview
.
Comments
Post a Comment