Android toolbar showing images of previously added fragment - android

I used toolbars in fragments instead of inflating it to activity.I am using different ImageViews in each toolbar layout.I was working fine. When I changed fragment transaction replace by add it is showing images of current layout images and previous fragment's images also.transaction.add(R.id.fragment_container, newFragment);What can I do?
I replaced replace by add since it showing some transparency while animating the second fragment from bottom.
This is how I added second fragment,
Fragment newFragment = new DetailsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_up, R.anim.splashfadeout);
transaction.add(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();

if You use image different image in different fragment then you must hide or remove resourse onDestroy like it.
#Override
public void onDestroy() {
super.onDestroy();
MainActivity activity = (MainActivity) getActivity();
activity.mToolbarRightImageView.setVisibility(View.GONE);
}
if in which fragment you not use that ToolBarimage then on create of fragment you also hide that image.I faced this problem but i solved this way
MainActivity activity = (MainActivity) getActivity();
activity.mToolbarRightImageView.setVisibility(View.GONE);
If You want to use toolBar ImageView in each fragment , You make public in Mainactivty then you use above instruction

Related

How to open another fragment from inside a ViewPager fragment?

I followed this tutorial to build an app with three fragments in sliding view (ViewPager).
https://c1ctech.com/android-viewpager-example-to-create-sliding-screens/
The app was built successfully. Then I added a button inside FragmentOne. And also created another fragment named FragmentFour with fragment_four.xml layout file.
Now I want to link the FragmentFour with FragmentOne through onClickListener with the button I created before. When in FragmentOne the button will be clicked, and it'll launch FragmentFour. To achive that, inside onClickListener I added these codes-
FragmentFour fragFour = new FragmentFour();
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_frameLayout, fragFour);
transaction.addToBackStack(null);
transaction.commit();
But after running the app, when I click on that button from inside the FragmentOne, FragmentFour's layout overlaps over the FragmentOne's layout. And FragmentTwo and FragmentThree can be accessible via sliding from FragmentFour, but I don't want this.
If anything wrong here, please help me.
Your code is replacing FragmentOne with FragmentFour in the ViewPager:
transaction.replace(R.id.fragment_one, fragFour);
which results in a ViewPager with FragmentFour, FragmentTwo, and FragmentThree
It seems like you want to start a new Activity for FragmentFour. From your onClick handler in FragmentOne, start a new activity as follows:
getActivity().startActivity(new Intent(context, FragmentFourActivity.class));
FragmentFourActivity is an Android Activity (e.g. AppCompatActivity) whose layout contains FragmentFour. Follow along at https://developer.android.com/training/basics/firstapp/starting-activity if unsure how to do so.
The problem is now gone. I just need to make few changes to my codes. Inside onClickListener, I changed my codes like this-
Before:
FragmentManager fragmentManager = getChildFragmentManager();
After:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
And inside the fragment_four.xml file's relative layout:
android:background="#color/bgcolor"
android:clickable="true"
android:focusable="true"

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.

Activity Buttons still ACTIVE when replaced by Fragment

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.

android:onclick="" - to call a layout

I'm wondering if it would be possible, to use the XML-onclick attribtue to call another XML-"View".
Means if I'm in a menu and click on the button "Create new Drawing", shouldn't I be able to call just another XML-View to bring up more options?
What I know is: android:onclick="hellothere" calls the method public void hellothere(View view) in the Activity to whom the view belongs, but why does it has to be used with code, instead that I'd just call another XML-Layout?
Example: andorid:onclick="new_page" (new_page.xml opens)
Calling another xml without a context is not possible in android. The context would be something like an activity, fragment etc.
If you want to inflate another xml into an area on your desired layout without activity transition, use fragments instead.
Heres's a simple code how to attach a fragment in a specific or generic area:
attaching to generic fragment area:
FragmentTransaction ft;
Fragment mFragment = new MySampleFragment();
ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, mFragment, "samplefragment");
ft.commit();
as you can see above, we attach the Fragment into the default fragment inflater of android which is "android.R.id.content".It gives you the root element of a view, without having to know its actual name/type/ID. Check out Get root view from current activity
attaching to specific fragment area:
FragmentTransaction ft;
Fragment mFragment = new MySampleFragment();
currentFragment = mFragment;
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.myfragmentid, mFragment, "samplefragment");
ft.commit();
Here we inflated the fragment inside a specific area with an actual ID which is R.id.myfragmentid. In this case you can specify which area in your page you will show your desired output upon a specific events like button click, hover, etc.
Hope it helps. Cheers! :)

Android Add Fragment Without Transaction

I'm trying to replace a Fragment with another Fragment dynamically in my activity.
It looks like you can't replace a fragment statically defined in a layout file, with a dynamically created fragment:
Android: can't replace one fragment with another
The suggested solution was to add the original Fragment dynamically in the onCreate method:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ShelfFragment shelves = new ShelfFragment();
ft.add(R.id.left_fragment, shelves);
ft.addToBackStack(null);
ft.commit();
}
This works, but when the user presses the back button, the original Fragment is removed instead of closing the Activity because the FragmentTransaction added it to the FragmentManager stack.
Is there a way to add the initial Fragment to my activity without a Transaction/Stack entry?
Don't add it the backstack. Delete the ft.addToBackStack(null); line, you only need this if you want to be able to go back to the previous state with the back button.

Categories

Resources