How to provide back button in fragments programmatically? - android

I have one Activity with two fragments.here i provide one back button in 2nd fragment programatically to goback to 1st fragment.
here my fragment1 code:
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
V = inflater.inflate(R.layout.lm_fragement, container, false);
listview = (ListView)V.findViewById(R.id.listView1);
url="http://victorysites.org/apps/SSPC/homeinfo.php?UDID=12341234";
JsonParsing json=new JsonParsing();
list=json.getData(url);
System.out.println("length :"+list.size());
listview.setAdapter(new MyAdapter(getActivity(),list));
final ProgressDialog waitdialog;
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Bundle data = new Bundle();
data.putString("name",""+arg2);
Fragment f;
f = new PM_Fragment();
f.setArguments(data);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
Button bt=(Button)V.findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle data = new Bundle();
data.putString("name","Venkat");
Fragment f;
f = new PM_Fragment();
f.setArguments(data);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
return V;
}
}
Here my fragment2 code:
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
f=new PM_Fragment();
V = inflater.inflate(R.layout.pm_fragment, container, false);
Bundle bundle = this.getArguments();
String myInt = bundle.getString("name");
System.out.println("NAME"+myInt);
TextView tv=(TextView)V.findViewById(R.id.portrait_message);
tv.setText(myInt);
Button bt=(Button)V.findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*Fragment f=new PM_Fragment();
getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();*/
//getActivity().getFragmentManager().beginTransaction().remove(this).commit();
getFragmentManager().popBackStack();
}
});
return V;
}
}
its working fine,but the problem is it reloads the previous(1st fragment) screen.But i don't want reload the page,i want its working like default back button in emulator.
Anyone can give me an idea. Thanks in advance.

When using fragments in your app, individual FragmentTransaction objects may represent context changes that should be added to the back stack. If you are implementing a master/detail flow on a handset by swapping out fragments, you should ensure that pressing the Back button on a detail screen returns the user to the master screen. To do so, call addToBackStack() before you commit the transaction:
// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();
*When there are FragmentTransaction objects on the back stack and the user presses the Back button, the FragmentManager pops the most recent transaction off the back stack and performs the reverse action (such as removing a fragment if the transaction added it).*
Note: You should not add transactions to the back stack when the transaction is for horizontal navigation (such as when switching tabs) or when modifying the content appearance (such as when adjusting filters). For more information, about when Back navigation is appropriate, see the Navigation design guide.
ADDED
If your application updates other user interface elements to reflect the current state of your fragments, such as the action bar, remember to update the UI when you commit the transaction. You should update your user interface after the back stack changes in addition to when you commit the transaction. You can listen for when a FragmentTransaction is reverted by setting up an FragmentManager.OnBackStackChangedListener:
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
// Update your UI here.
}
});
Please refer this link for more details

Related

Cannot able to open second fragment

On click of Button I am trying to open second fragment(YourResultFragment.java),
I had a lots but don't know why it is not working , here is my code which I am using
public class KnowYourBodyFragment extends Fragment {
public KnowYourBodyFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_know_your_body, container, false);
Button button = (Button)rootView.findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Fragment mFragment = new YourResultFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.know_your_body_container, mFragment ).commit();
}
});
// Inflate the layout for this fragment
return rootView;
}
Use this Try to replace it with Default Container android.R.id.content.
Button button = (Button)rootView.findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment newFragment = new YourResultFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
// Replace whatever is in the fragment_container view with this
// fragment,
// and add the transaction to the back stack
transaction.replace(android.R.id.content, newFragment);
transaction.addToBackStack("tag");
// Commit the transaction
transaction.commitAllowingStateLoss();
}
});
What is transaction.addToBackStack("tag") ?
Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

Saving Fragment State/Data from Fragment to Fragment

I'm trying to figure out how to save user inputted data on a fragment if they leave it and come back. I have one Activity that several Fragments work off of. All the examples I find are saving the state when leaving the Fragment and Activity. I'm never leaving the Activity; therefore onSaveInstanceState within the Fragment is never called. onPause is called, but I'm unable to figure out how to save the state within there.
This app displays a list of tickets. When the user taps a ticket, it grabs preliminary information from the local DB for read only fields and allows the user to input the ticket information before submitting. I'm trying to allow a user to tap on a ticket, enter data, leave and come back to that ticket without losing information entered.
Out of all the examples I've studied on this, it seems to me I can use Fragmenttransaction to save the Fragment's ID. Below is the main Activity that all fragments are attached to:
public class Activity_Main extends Activity
implements Fragment_OpenTickets.INT_OnOpenTicketSelectedListener,
Fragment_SubTickets.INT_OnSubTicketSelectedListener,
Fragment_Ticket.INT_SubmitNewTicket {
DBController dbc;
Fragment fr;
boolean gps_enabled = false;
boolean network_enabled = false;
HashMap<String, String> addInfo;
RadioButton btn_radio_open;
RadioButton btn_radio_submitted;
RadioButton btn_radio_settings;
String ticketDate, ticketTime;
int m, d, y, hr, min;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_radio_open = (RadioButton)findViewById(R.id.radio_open);
btn_radio_submitted = (RadioButton)findViewById(R.id.radio_submitted);
btn_radio_settings = (RadioButton)findViewById(R.id.radio_settings);
dbc = new DBController(this);
if (dbc.checkCredentials() != null) {
fr = new Fragment_OpenTickets();
} else {
fr = new Fragment_Settings();
btn_radio_open.setChecked(false);
btn_radio_submitted.setChecked(false);
btn_radio_settings.setChecked(true);
}
FragmentManager fm = getFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_place, fr);
ft.addToBackStack(null); // For back button action.
ft.commit();
// Start scheduler service.
this.startService(new Intent(this, Service_Send.class));
// Start GPS service.
startGPS();
}
/**
* Top menu buttons.
*/
public void selectFrag(View view) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
switch (view.getId()) {
case R.id.radio_open:
//fr = new Fragment_OpenTickets();
ft.replace(R.id.fragment_place, new Fragment_OpenTickets());
ft.addToBackStack(null);
btn_radio_submitted.setChecked(false);
btn_radio_settings.setChecked(false);
ft.commit();
break;
case R.id.radio_submitted:
//fr = new Fragment_SubTickets();
ft.replace(R.id.fragment_place, new Fragment_SubTickets());
ft.addToBackStack(null);
btn_radio_open.setChecked(false);
btn_radio_settings.setChecked(false);
ft.commit();
break;
case R.id.radio_settings:
//fr = new Fragment_Settings();
ft.replace(R.id.fragment_place, new Fragment_Settings());
ft.addToBackStack(null);
btn_radio_open.setChecked(false);
btn_radio_submitted.setChecked(false);
ft.commit();
break;
}
}
/**
* Open ticket selection action.
*/
public void onOpenTicketSelected(HashMap position) {
// Create fragment and give it an argument for the selected ticket.
fr = new Fragment_Ticket();
Bundle args = new Bundle();
args.putSerializable("HashMap", position);
fr.setArguments(args);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_place, fr);
ft.addToBackStack(null); // For back button action.
ft.commit();
}
/**
* Submitted ticket selection action.
*/
public void onSubTicketSelected(HashMap position) {
// Create fragment and give it an argument for the selected ticket.
fr = new Fragment_SubTicket();
Bundle args = new Bundle();
args.putSerializable("HashMap", position);
fr.setArguments(args);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null); // For back button action.
fragmentTransaction.commit();
}
Any help will be very appreciated. If I need to post more code then please let me know. Thank you!
I think you should store ticket info in your Activity when users leave the fragment and get the info back and display it when you come back.
In Activity_Main class, you create 2 methods:
public void saveTicketInfo(TicketInfo info) {
// save info here
}
And:
public TicketInfo getTicketInfo() {
// get info here
}
In your fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
// get ticket info and display it
Activity_Main mainActivity = (Activity_Main)getActivity();
TicketInfo info = mainActivity.getTicketInfo();
if (info != null) {
// handle display info here
}
...
}
#Override
public void onDestroyView() {
TicketInfo info = new TicketInfo();
// get info from view and store in the object
Activity_Main mainActivity = (Activity_Main)getActivity();
mainActivity.saveTicketInfo(info)
super.onDestroyView();
}
I posted a lengthy detailed answer on SO # Passing data between fragments contained in an activity. Search for my answer on it. This is a safe way to pass data from Fragment to Activity. And then you can easily pass that data from Activity to the Fragment's constructor or public method.

how to call two different fragment from a fragment

I have a Fragment Class
and I have a linear layout clickable
and a button clickable.
So I want to know that when I click linear layout it should start fragment 1
and when I click button it should start fragment 2
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
LinearLayout l = (LinearLayout) rootView.findViewById(R.id.line1);
l.setOnClickListener(this);
Button b = (Button) rootView.findViewById(R.id.map_button);
b.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
Fragment frag = new Fragment1();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container, frag);
ft.addToBackStack(null);
ft.commit();
Fragment frag = new Fragment2();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container, frag);
ft.addToBackStack(null);
ft.commit();
Add a onClickListener on your button and try that to show your fragment when it is clicked.
#Override
public void onClick(View v) {
Fragment fragment = new Fragment1();
FragmentTransaction fragmentTr = getFragmentManager().beginTransaction();
fragmentTr .setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
if (fragment.isHidden())
fragmentTr.show(fragment);
}
Edit:
You can get which view is clicked with a switch case statement:
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.Button:
// show fragment x
case R.id.LinearLayout:
// show fragment y
}
}
your onClick method has to know what was clicked when you say setOnClickListener(this) you refer to the onClick method of the class .in your code it doesn't matter what the user clicks the whole onClick method will be executed . in order to specify the right block of code to be executed you can use Switch and case.

In fragment screen keeps on loading again and again?

I have showing list of items which is displayed in grid and the data is coming from webservice . And when i click on the item it takes to the new screen but when coming from this screen to the screen having Grid the layout is recreated but i want to save the instance like activity so that the screen is not created again. what i can do to achieve this ?
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mfragmentManager = getActivity().getSupportFragmentManager();
mhandler=new Handler(this);
mLvAllDeals = (GridView) mview.findViewById(R.id.xLvAllDeals);
mLvAllDeals.setCacheColorHint(0);
mLvAllDeals.setOnItemClickListener(this);
new MyDealsAsyncTask().execute();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAlDealName = new ArrayList<AllDealsData>();
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(arg0==mLvAllDeals)
{
position = arg2;
AllDealsData bean;
bean = mAlDealName.get(position);
Fragment frag = getFragmentManager().findFragmentById(R.id.inner_content2);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
if (!frag.getTag().equals("dealsinfo"))
{
Bundle args = new Bundle();
args.putString("dealid", bean.getId());
mdealinfo.setArguments(args);
ft.replace(R.id.inner_content2, mdealinfo, "dealsinfo");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
}
}
}
When performing transactions in fragments, onCreateView() and onActvityCreated() will be called again but it wont be called onCreate() method. So maintain data in onCreate() method. If data is already loaded, set directly to adapter as well as loading webservices data manage it using some extra flag.
For more information on fragment life cycle refer this link

Android replace the current fragment with another fragment

I just started with fragment design for HoneyComb. I created two fragments. When i click a button in the left side fragment, a new fragment is created in right side. Meanwhile when i click a button in the right fragment(ie. DetialsFragment in my code below should be replaced by another fragment.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment class="com.fragment.example.Titles"
android:id="#+id/titles" android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<FrameLayout android:id="#+id/details" android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>
FragmentExample.java
public class FragmentExample extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Titles.java
public class Titles extends Fragment {
public FragmentTransaction ft;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main1, null);
Button button1 = (Button)v.findViewById(R.id.button1);
button1.setText("santhosh");
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DetailsFragment details = (DetailsFragment)
getFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != 1) {
// Make new fragment to show this selection.
details = DetailsFragment.newInstance(1);
// Execute a transaction, replacing any existing
// fragment with this one inside the frame.
ft
= getFragmentManager().beginTransaction();
ft.add(R.id.details, details, "detail");
ft.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
});
return v;
}
}
DetailsFragment.java
public class DetailsFragment extends Fragment {
/**
* Create a new instance of DetailsFragment, initialized to
* show the text at 'index'.
*/
Titles title = new Titles();
String[] titles = {"Title1", "Title2", "Title3", "Title4"};
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
// Currently in a layout without a container, so no
// reason to create our view.
return null;
}
Button button = new Button(getActivity());
button.setText("Next");
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return button;
}
}
Then provided your button is showing and the click event is being fired you can call the following in your click event:
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();
and if you want to go back to the DetailsFragment on clicking back ensure you add the above transaction to the back stack, i.e.
ft.addToBackStack(null);
Or am I missing something? Alternatively some people suggest that your activity gets the click event for the button and it has responsibility for replacing the fragments in your details pane.
Latest Stuff
Okay. So this is a very old question and has great answers from that time. But a lot has changed since then.
Now, in 2020, if you are working with Kotlin and want to change the fragment then you can do the following.
Add Kotlin extension for Fragments to your project.
In your app level build.gradle file add the following,
dependencies {
def fragment_version = "1.2.5"
// Kotlin
implementation "androidx.fragment:fragment-ktx:$fragment_version"
// Testing Fragments in Isolation
debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
}
Then simple code to replace the fragment,
In your activity
supportFragmentManager.commit {
replace(R.id.frame_layout, YourFragment.newInstance(), "Your_TAG")
addToBackStack(null)
}
References
Check latest version of Fragment extension
More on Fragments
You can try below code. it’s very easy method for push new fragment from old fragment.
private int mContainerId;
private FragmentTransaction fragmentTransaction;
private FragmentManager fragmentManager;
private final static String TAG = "DashBoardActivity";
public void replaceFragment(Fragment fragment, String TAG) {
try {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(mContainerId, fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commitAllowingStateLoss();
} catch (Exception e) {
// TODO: handle exception
}
}
Use android.support.v4.app for FragmentManager & FragmentTransaction in your code, it has worked for me.
DetailsFragment detailsFragment = new DetailsFragment();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.details,detailsFragment);
fragmentTransaction.commit();
If you have a handle to an existing fragment you can just replace it with the fragment's ID.
Example in Kotlin:
fun aTestFuction() {
val existingFragment = MyExistingFragment() //Get it from somewhere, this is a dirty example
val newFragment = MyNewFragment()
replaceFragment(existingFragment, newFragment, "myTag")
}
fun replaceFragment(existing: Fragment, new: Fragment, tag: String? = null) {
supportFragmentManager.beginTransaction().replace(existing.id, new, tag).commit()
}
Updated Answer (Working for Overlap problem as well)
#aniket-thakur(https://stackoverflow.com/users/2396539/aniket-thakur) gave correct answer. Thank you for that!
But, getFragmentManager() is deprecated, so following code did work for me.
final FragmentTransaction ft = getParentFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new GalleryFragment(), "NewFragmentTag");
ft.addToBackStack(null);
ft.commit();
it's very simple how to replace with Fragment.
DataFromDb changeActivity = new DataFromDb();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.changeFrg, changeActivity);
transaction.commit();

Categories

Resources