Activity Buttons still ACTIVE when replaced by Fragment - android

Basically, I want to open a fragment from Activity. Everything worked fine (activity button opens the fragment and the fragment shows), until I miss-clicked outside the fragment's layout where one of the Activity button is located and it did its onClick method. That's why I wondered why is my Activity still active.
this is how I open the fragment from Activity
I tried using .add instead of .replace still the same.
#OnClick(R.id.login_register_lbl)
public void registerAction(View view) {
// TODO register transition...
FragmentManager fragmentManager = (LoginActivity.this.getFragmentManager());
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
RegisterFragment fragment = new RegisterFragment();
fragmentTransaction.replace(R.id.fragment_register_holder, fragment);
fragmentTransaction.commit();
}

In fragment layout in root element set "background" , "clickable=true" & Focusable="true".

In my opinion you can achieve this functionality by 2 ways:
Open a dialog fragment in full screen mode to avoid accidentally clicks on activities views.
If you wanted to show both fragment's and activity's views at the same time then, on click of your activity's button which opens the fragment, disable it by yourbutton.setenabled(false); and when you wanted to close the fragment enable that button again.

Related

Fragments overlapping when I am using fragmentTransaction.add method

I am facing some weird issue with fragments, I am displaying google map on these two fragments, while switching from Home Fragment to Request cab fragment, in Request cab fragment, fragments are overlapping, I can see Home Fragment in background and its also clickable I can move map of Home Fragment too. This issue occur when I use fragmentTransaction.add(R.id.frame, fragment, fragmenttag);
but if I use replace then its working fine, but I want to get the view back onBackPressed that is not possible with replacing the fragmnet. I am attaching screenshots and code.
public void changeFragment(final Fragment fragment, final String fragmenttag) {
try {
drawer_close();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction().addToBackStack(null);
fragmentTransaction.add(R.id.frame, fragment, fragmenttag);
fragmentTransaction.commit();
} catch (Exception e) {
}
}
You just need to add background when you add fragment.
add(): adds the new fragment on the top of another fragment that's why you can see below fragment
replace(): removes everything then adds the new fragment
add and replace work quite differently, while replace removes current fragment from inflated view group. add method just adds new layout inside of ViewGroup. thats why you can see your fragment and thats why its clickable just think them as normal views and not fragments. now you could just add white background to root layout of second fragment and make it clickable it should solve all your problems.

how to restore last opened fragment inside an activity after getting back from another actvivty

I'm using multiple fragments inside an activity like this flow: Fragment A has a list on it's item click Fragment B opens and it also has a list opens Fragment C which has a list Open another Activity , The problem is when I go back from the other Activity I found Fragment A is opened, How I restore the last Fragment C when go back from the other activity?
here is the code of replacing Fragment inside my activity
protected void showFragment(Fragment fragment) {
String TAG = fragment.getClass().getSimpleName();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container_home, fragment, TAG);
fragmentTransaction.addToBackStack(TAG);
fragmentTransaction.commit();
}
replace removes the existing fragment and adds a new fragment. This means when you press back button the fragment that got replaced will be created with its onCreateView being invoked. Whereas add retains the existing fragments and adds a new fragment that means existing fragment will be active.
Use add instead of replace
fragmentTransaction.add(R.id.container_home, fragment, TAG);
I had the same issue solved using the above code.
You can use
fragmentTransaction.addToBackStack(null);
instead of,
fragmentTransaction.addToBackStack(TAG);
Hope Your problem will solve.

Fragments added to backstack works perfectly fine, but once it's back on the previous fragment the second fragment does not inflate

I have initially added a fragment to my activity. There is a button, whose Onclickmethod replaces the previous fragment with the new one. Once I press the back button it gets back to the previous fragment, so far so good. But when a once again try to click that button, the next fragment doesn't get inflated. Why is that so?
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
MainFragment fragment = new MainFragment();
fragmentTransaction.add(R.id.mainFragment,fragment);
fragmentTransaction.commit();
The above piece of code is in OnCreate() and I have added the OnclickListener to the button from the first fragment in onPostResume() method, as shown in the following piece of code from the OnClickListner method.
fragmentTransaction = fragmentManager.beginTransaction();
TakeDataFragment fragment=new TakeDataFragment();
fragmentTransaction.replace(R.id.mainFragment,fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
It works perfectly fine once, but after getting back from the second fragment on the first one, it does not inflate the second fragment even on button click event.
The issue has been resolved, I was using the callback methods of an activity to set the onClickListners to the buttons that were inside the first fragment. Now I have used the onResume() call back method of the fragment and works fine. Thanks!

Android: How to start a new fragment without destroying the state of the previous fragment?

How to replace Fragment_1 Fragment_2 with the ability to return back to the state and Fragment_1 (scroll, inputs, forms, other)?
enter image description here
If you use Activities, then you can use the method startActivityForResult. The work of this method perfectly shows the current task with fragments.
I do not want to save the values and then substitute them. We need that to Fragment_1 remained intact.
How to save the status of previous fragments at the opening of the new fragment_N and when you turn the screen using backstack?
Is there a ready library for the implementation of these tasks?
Instead of replacing fragment, You can add a new fragment and hide current one.
Something like this
FragmentTransaction t = getFragmentManager.beginTransaction();
t.hide(your_current_fragment);
t.add(container, new_fragment);
t.addToBackStack(TAG);
t.commit();
It will not loss the state of hidden fragment
FragmentTransaction t = getFragmentManager.beginTransaction();
t.hide(your_current_fragment);
t.add(container, new_fragment);
t.addToBackStack(TAG);
t.setCustomAnimations(R.anim.open_enter,R.anim.close_exit,R.anim.open_enter,R.anim.close_exit)
or
t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
t.setTrans
t.commit();
replace FragmentPagerAdapter with a HorizontalScrollView contains 2FrameLayout for fragment.You can freely change fragment in FrameLayout.

android add fragment advice

In my application I have two fragments A and B. A fragment contains Google map and listview inside on it B fragment Google map only.
I want open B fragment from A fragment but when I press back button it has to go to A fragment without loading data.
public void addPage(final DefaultFragment pDefaultFragment, final boolean isAddToBackStack){
FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.add(R.id.content_frame, pDefaultFragment);
//transaction.replace(R.id.content_frame, pDefaultFragment);
if (isAddToBackStack) transaction.addToBackStack(null);
transaction.commitAllowingStateLoss();
}
I use this code for adding fragment
Bframent b = new Bfragment();
addPage(b,true);
I know differences between transaction. add and transaction.replace. My problem is when I use transaction.add A fragment's map doesn't destroy it stays above of B fragment's map when pressing button. But when use transaction.replace after press back button data loads again.
So could anyone tell me what should I do to Press back button without to load data without any problem on view also.
On back press just provide your fragment null;

Categories

Resources