Previous button in Android application -


i developing simple android application. have fragment 2 buttons: prev , home. if touch home works fine (back main menu) must touch twice prev button if want previous activity. don't know going on. searched nothing. has got same problem?

thanks help.

here code:

  @override   public view oncreateview(layoutinflater inflater, viewgroup container,                        bundle savedinstancestate) { view view = inflater.inflate(r.layout.navigation_activity_fragment, container, false); textview textview = (textview) view.findviewbyid(r.id.activitytitle); button home = (button) view.findviewbyid(r.id.home); button prev = (button) view.findviewbyid(r.id.button2);  home.setonclicklistener(new onclicklistener(){      @override     public void onclick(view v) {         intent = new intent(getactivity(),  mainmenuactivity.class);         startactivity(i);                 }  }); prev.setonclicklistener(new onclicklistener(){      @override     public void onclick(view arg0) {         getactivity().finish();                 }  });   return view;  } 

and xml:

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"             android:id="@+id/relativelayout1"             android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:background="#00000000"             android:orientation="vertical">  <imageview android:id="@+id/screen_header" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/screen_header" android:contentdescription="@null"/>  <textview   android:id="@+id/activitytitle"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_alignbottom="@+id/screen_header"   android:layout_centerhorizontal="true"   android:layout_marginbottom="14dp"   android:text="large text"   android:textappearance="?android:textappearancelarge"   android:textcolor="@color/black"   android:textsize="16.5dip"   android:textstyle="bold" />  <button   android:id="@+id/home"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_alignbaseline="@+id/activitytitle"   android:layout_alignbottom="@+id/activitytitle"   android:layout_alignparentright="true"   android:background="@drawable/home_btn"    android:onclick="onclick"/>  <button   android:id="@+id/button2"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_alignbottom="@+id/activitytitle"   android:layout_alignparentleft="true"   android:background="@drawable/prev_btn"    android:onclick="onclick"/>   </relativelayout> 

this general way -

you have add fragments transaction whenever need display them in ui.

for instance -

from fragment a, go fragment b, replace a b , use addtobackstack() before commit().

now fragment b, use popbackstack(), bring a.

prev.setonclicklistener(new onclicklistener(){      @override     public void onclick(view arg0) {         getactivity().getsupportfragmentmanager().popbackstack();                  } }); 

for code particularly not sure not correct way go previous fragment. when getactivity().finish(), finishing current activity rather going previous fragment. please note when each activity have single fragment tried seems correct if activity contains many fragments , perform finish on switch previous activity.

so in order enable navigation fragments, have add them backstack.

then go previous fragment use popbackstack.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -