View Overlaps when back button pressed in fragment - android

In my app i have one activity and multiple fragments,From Activity A onCreate() i just show my HomeFragment and after user click events on my HomeFragment it moves to different fragment Like AboutFragment,Galleryfragment etc.
My problem is that when user move like ->HomeFragment -> AboutFragment ->GalleryFragment amd hen press virtual back button then view of GalleryFragment and AboutFragment overlaps each other.
I am using following code when move between fragment-
FragmentTransaction ft = fragmentManager.beginTransaction();
Fragment mFragment = new AboutFragment();
ft.replace(R.id.content_frame, mFragment);
ft.addToBackStack(null);
ft.commit();
So how can i solve this issue.
and i saw following eror in logcate-
FATAL EXCEPTION: main
java.lang.IllegalStateException: Fragment already added: AboutFragment{41c8a878 #0 id=0x7f07003d}
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1175)
at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:722)
at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1516)
EDIT:
my onCreateView of AboutFragment is-
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.aboutview, container,false);
aboutView = (TextView) view.findViewById(R.id.about_textview);
return view;
}
thanks in advance.

Related

Fragment button not opening new activity

I've incorporated a horizontal slide navigation component (which required making the class extend Fragment). The slide part works fine. Here i have respective onClick() buttons which open a new activity. If I add a button into one of those activities I'm not finding a way to have the displayed activity subsequently refresh. I would think inflating an activity from a button within a fragment would be possible but anything I try stops the emulator.
There's not much to my code so far, so I'm not going to clutter my question with the associated layout part. Any help is certainly appreciated.
Fragment #1’s Java code
public class TasksFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tasks, container, false);
Button ID = (Button) rootView.findViewById(R.id.button_create_appraisal_rpt);
ID.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AppraisalReportActivity appraisalRptAct = new AppraisalReportActivity();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.child_fragment, appraisalRptAct);
fragmentTransaction.setTransition(fragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return rootView;
}
}
Fragment #1 (portion), which has the button that should initiate aa refresh of this Fragment #1 with Fragment #2
Fragment #2 that's to supersede/refresh the previous fragment when Fragment #1's button is clicked
I don't really understand what you mean. Do you want to click Button in Fragment to jump to another Activity?
If it is,I think maybe you can try use getActivity() to make Activity are working.
for example:
getActivity().startActivity(new Intent(getActivity(),Activity.class))
I hope my answer will help you

Fragment callback on attach complete in Activity

Is it possible to get the attach complete of a fragment in the hosting activity? I am new to Android.
Here is my code to add a fragment:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
DecorationFragment fragment = new DecorationFragment();
ft.add(R.id.fragmentContainer,fragment,"decoration_fragment");
ft.commit();
I have 2 objects in the Fragment. I want to handle their click event in the Activity.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_decoration, container, false);
mChangeTextColorButton = (Button) rootView.findViewById(R.id.changeTextColorButton);
mColorPallette = (LinearLayout)rootView.findViewById(R.id.color_pallette_linearView);
return rootView;
}
How to achieve this?
Not sure exactly what you're asking, but perhaps this is what you're looking for:
https://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#onAttachFragment(android.support.v4.app.Fragment)
void onAttachFragment (Fragment fragment)
Called when a fragment is attached to the activity.
This is called after the attached fragment's onAttach and before the attached fragment's onCreate if the fragment has not yet had a previous call to onCreate.

How to open fragment on a button click from an activity either with intent and without intent in android? [duplicate]

This question already has answers here:
How to open a Fragment on button click from a fragment in Android
(3 answers)
Closed 6 years ago.
I tried the following code:
Intent in= new Intent(Activity1.this,Fragment.class);
startactivity(in);
This is not how fragments work, fragments must be attached to an Activity. To get your desired effect you must either start a new Activity that contains the fragment you wish to show, or display the new fragment in the current Activity.
In order to decide between which approach to take, I would consider how you want the Fragment to affect the navigation of your interface. If you want the user to be able to get back to the previous view by using the Back button, you should start a new Activity. Otherwise you should replace a view in your current Activity with the new Fragment.
Though, it is possible to add a Fragment to the back stack, I would only attempt to do so if you are confident with the structure of your user interface.
To show a new fragment in the current Activity you can use a FragmentTransaction:
Fragment fragment = CustomFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_layout, fragment).commit();
write this code in your onCreate or in your intent:
FragmentManager fm = getSupportFragmentManager();
YourFragment fragment = new YourFragment();
fm.beginTransaction().add(R.id.main_contenier,fragment).commit();
Fragments not Open through Intent.
You should use Fragment manager.
Fragment fragment= new YourFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment); // fragment container id in first parameter is the container(Main layout id) of Activity
transaction.addToBackStack(null); // this will manage backstack
transaction.commit();
Sample Example of Fragment
public class MyFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_my, container, false);
Button button1= (Button) view.findViewById(R.id.button1_Id);
Button button2= (Button) view.findViewById(R.id.button2_Id);
return view;
}
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment= new YourFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment); // fragmen container id in first parameter is the container(Main layout id) of Activity
transaction.addToBackStack(null); // this will manage backstack
transaction.commit();
}
});
}

Android. Navigation drawer. Backstack issues in Fragment

I am trying to implement app with navigation drawer.
I created application on Eclipse.
This is default(or template) navigation drawer application.
MainActivity has root FrameLayout which includes different fragments at different times.
By default, it contains fragment A.
When item B will be chosen from navigation drawer, it will replace fragment A to fragment B.
MainActivity:
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, fragmentB);
transaction.addToBackStack(null);
transaction.commit();
FragmentB is root fragment for fragmentB2, FragmentB3 etc.
I thought that in this way, it will be easier to save fragments state.
It is saving fragment state only for FragmentB2.
FragmentA(root A) onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* Inflate the layout for this fragment */
View view = inflater.inflate(R.layout.fragment_balance_root, container,
false);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.balance_root_frame, MainActivity.balanceMainFragment);
transaction.addToBackStack(null);
transaction.commit();
return view;
}
FragmentA2 OnCreateView():
View rootView = inflater.inflate(R.layout.fragment_balance_main,
container, false);
return rootView;
FragmentB(root B) onCreateView():
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.tariffs_root_frame, MainActivity.tariffsMainFragment);
transaction.addToBackStack(null);
transaction.commit();
FragmentB2:
private static void goToTariffInfo() {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.tariffs_root_frame, new TariffsInfoFragment());
transaction.addToBackStack(null);
transaction.commit();
}
Fragment B3:
View rootView = inflater.inflate(R.layout.fragment_tariffs_info,
container, false);
return rootView;
I have many questions, but they are related to each other.
1. How to avoid saving root fragment in backstack?
When I press back button on Fragment A2 it goes to A1 which is blank.
I have tried to remove addToBackStack from MainActivity. This caused to quit on only one press back button.
I have tried to remove addToBackStack from FragmentA. No effect.
2.How to set seperate backstack for different collections of fragments?
For example: Navigation drawer includes 2 items: ItemA and ItemB.
Each item can be considered as one section.
FragmenA->FragmentA2->FragmentA3
FragmentB->FragmentB2->FragmentB3
But in my situation, when I click on back button on Fragment B2, somehow app goes back to Fragment A2(which previously was opened)
Even on Fragment B3, back button causes to go to Fragment A2.
How to solve these problems?

Restore inner fragment

I'm making an app that in a certain FragmentActivity has a Tabhost hosting 2 Fragments.
One of them must have either a layout A showing up or a layout B showing up, while the other one has always the same layout.
FragmentActivity
Fragment1 (first tab)
LayoutA
LayoutB
Fragment2 (second tab)
LayoutC
In order to wrap the control of both layouts in separate modules, I made two Fragments, namely FragmentA and FragmentB, that use LayoutA and LayoutB respectively, making the activity look like this:
FragmentActivity
Fragment1 (first tab)
FragmentA
LayoutA
FragmentB
LayoutB
Fragment2 (second tab)
LayoutC
The problem I have is that I cannot make this stable against both:
the user leaving the app when first tab is shown
the user navigates to tab 2 and then back to tab 1
At first my code for Fragment1 looked like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = new FrameLayout(getActivity());
view.setId(1);
return view;
}
#Override
public void onStart() {
super.onStart();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(1, mCurrentFragment, "f1");
ft.commit();
}
#Override
public void onStop() {
super.onStop();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.remove(mCurrentFragment);
ft.commit();
}
Inside onStop I remove the fragment in order to attach it to the new FrameLayout created in OnCreateView when the Fragment restarts next time.
The problem with this code is that apparently I cannot do any transactions when user leaves the app, so it crashes with.
java.lang.RuntimeException: Unable to stop activity {my.package/my.package.MainTabHost}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
To solve the problem, I changed the removing of the fragment just before I try to add it to the FragmentTransaction in onStart:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = new FrameLayout(getActivity());
view.setId(1);
return view;
}
#Override
public void onStart() {
super.onStart();
FragmentTransaction ft = getFragmentManager().beginTransaction();
if(getFragmentManager().findFragmentByTag("f1") != null) {
ft.remove(mCurrentFragment);
}
ft.add(1, mCurrentFragment, "f1");
ft.commit();
}
This code has the other problem. Switching from the first tab to the second and then coming back to the first shows a blank layout for Fragment1.
Note: I set all fragments to retain their state.
Solved! I used the second code. I had to this sequence of steps inside onStart:
- detach the fragment
- add it to the new view
- attach it

Categories

Resources