Loading a fragment upon a specific condition - android

I have an issue please help me out. I am building an android application and I have successfully integrated Rewarded video ads to it.
I want to show a fragment or auto redirect to a specific fragment when the below condition is met:
#Override
public void onRewarded(RewardItem rewardItem) {
// I want to redirect the user to a particular fragment here.
}
So Please help me out, if there is any other way to achieve this then please let me know.

Use Fragment Manager and Fragment Transaction Manager for this.
getFragmentManager().beginTransaction().replace(R.id.your_framelayout_id, new your_fragment()).commit();

Write below code inside your onRewarded :
android.support.v4.app.Fragment fragment = new FragmentA();
android.support.v4.app.FragmentManager fragmentManager = getActivity()
.getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Here fragment_container is a FrameLayout defined inside your Activity on which you want to host your fragment.
I am mentioning a sample activity with a framelayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Below is a fragment Container" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_height="wrap_content"
android:layout_width="match_content" />
</LinearLayout>

Related

How do I programmatically add this fragment to my fragment?

I am trying to display a nested ListFragment inside my DialogFragment.
Apparently I cannot just declare a <fragment> in the XML for my DialogFragment layout because fragments-in-fragments need the childFragmentManager. So I am trying to do this in my DialogFragment:
Fragment listfragment = new ClassThatExtendsListFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(????????, listfragment).commit();
I have absolutely no idea what resource ID I need to put in the ???????? section, or how I'd even go about assigning it.
simply add FrameLayout in you layout. suppose you gave it's id as "container",
Fragment exampleFragment = new ExampleFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.container, exampleFragment).commit();
My requirement also was same like you. below code worked for me.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyTripFragment myTripFragment = new MyTripFragment();
fragmentTransaction.add(R.id.fragment_container, myTripFragment);
fragmentTransaction.commit();
XML code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Hope it will help for you.
I'm not sure what type of layout you're using for your DialogFragment, but generally in the XML that DialogFragment inflates you need to add a FrameLayout and importantly give it an ID. Then when you do your fragment transaction you pass in the resource id of that FrameLayout
XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
If you want to use nested fragments you'll need to call getChildFragmentManager():
FragmentManager fragmentManager = getChildFragmentManager()
Then for your fragment transaction:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new MyFragment()).commit();
You might want to use the add method instead of replace, but thats up to you
You might also want to add the previous fragment to the backstack if you want the enable back button presses:
fragmentTransaction.replace(R.id.fragment_container, new MyFragment()).addToBackStack(null).commit();

addToBackStack for fragment in Android

Say I have an activity and there are two placeholder in the view:
<RelativeLayout>
<RelativeLayout id="fg_1" ...>
<RelativeLayout id="fg_2 ...>
</RelativeLayout>
Now once something happened I will add a fragment to the view by these codes:
private void showFragment(Fragment fragment, int id) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(id, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
For example, when I trigger action 1, the Fragment1 will show, when trigger action2 Fragment2 will show.
Now when I click the back menu, in my opinion, the Fragment2 will disappear, and when I click back menu again, the Fragment1 will disappear, and the app will exit once I click back menu again.
However the app will exit even I click the back menu once, it seems that the addToBackStack does not work as I expected.
Did I miss anything?
I would like to say that you have to replace with same container.
Here you have taken 2 Relative layout as container.
<RelativeLayout>
<RelativeLayout id="fg_1" ...>
<RelativeLayout id="fg_2 ...>
</RelativeLayout>
Which should be like
<RelativeLayout>
<RelativeLayout id="fg_1" ...>
</RelativeLayout>
Replace your fragment with 1 Relative layout ( fg_1 ) container.
Let me know for the same if any issue.
Hope it will help you.
Edit:
Make sure you are using same FragmentManager, same Fragment. Means I have faced same issue before 2 months that I have used android.app.FragmentManager in one class and and android.support.v4.app.FragmentManager in other class.
May be it would be help you.
Remove the addBackStack(null) method from your code. after click on back press to exit the app.
private void showFragment(Fragment fragment, int id)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(id, fragment);
fragmentTransaction.commit();
}

Replaced fragment still received events

I am writing an android app using ActionBarSherlock
My layout file is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/fragment_menu"
android:layout_width="#dimen/menu_size"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/dummy"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Depending the category is selected in Menu fragment, I replace the fragment in dummy FrameLayout.Eg:
Bundle extras = new Bundle();
extras.putInt(ProgramDetailFrament.EXTRA_PROGRAM_ID, programId);
final ProgramDetailFrament fragment = ProgramDetailFrament.newInstance(extras);
getSupportFragmentManager().beginTransaction()
.replace(R.id.dummy, fragment)
.addToBackStack(null)
.commit();
getSupportFragmentManager().executePendingTransactions();
But the replaced fragment still receives touch/click event when I interact with the visible fragment. I don't know whether SherlockFragment is related to this issue?
I solved that by setting click event on the root layout of the visible fragment and do nothing in this event. But It seems a ugly solution.
Anyone knows how to solve it.
Thanks in advance.
As you state in your question, you're trying to replace a Fragment with another, so you should use the replace method of FragmentTransaction.
Here's roughly how to do it :
Bundle extras = new Bundle();
extras.putInt(ProgramDetailFrament.EXTRA_PROGRAM_ID, programId);
ProgramDetailFrament fragment = ProgramDetailFrament.newInstance(extras);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.id_of_fragment_container, fragment, DETAIL_FRAGMENT_TAG);
ft.commit();
I hope this helps ;-)
You actually need to use the replace function instead of add. What you're doing is adding a fragment on top of the other one, so you're creating a stack of fragments which are all still visible, only you don't see them because the top fragment covers all the other ones.
Use replace instead of add:
getSupportFragmentManager().beginTransaction()
.replace(R.id.dummy, fragment)
.addToBackStack(null)
.commit();
getSupportFragmentManager().executePendingTransactions();
This will remove all the other fragments in the dummy container and add the fragment you selected.

Implementing Fragments Androids

I am implementing fragments for the first time so please help me.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="#+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
I want that fragment having the id as 'list' should remain constant but the the fragment having id 'viewer' should be able to call different classes.
(Note that the classes extend Activity.)
My question is simple: I have four classes(Extending ACTIVITY). I want to divide the screen into two parts. The left Side remains constant which contains the listview. On list view's click I want to open my Class(Extending ACTIVITY), but only in the right portion(remaining screen).
It is a basic question. You should start from here. And this topic can help you also.
Fragments are like seperate acticities, so unless u make the changes the action on one fragment will not affect the other fragments.
Assuming u have a listview on the left fragment, in its activity place a onItemClickListener.
For each itemclick switch the activity on the right fragment.
Sample Code for the OnItemClick Event
Fragment fragment=new activity1();
fragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.frame2,fragment);
ft.commit();
In the above code segment activity1 is the new class want to attach to the right fragment. R.id.frame2 is the id of the framelayout that is used with the right fragment.
According to the android documentation of fragments:
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
http://developer.android.com/guide/components/fragments.html
what i understood from your question is that you want to the content of your fragement viewer at run-time. a possible solution which i can suggest for this is:
Instead of your four classes extending Activity, extend Fragment, each having its own layout. Modify the main layout file to look something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
the FrameLayout will basically act as a container for your fragments, which you can dynamically load at run-time(by clicking the ListView). This tutorial will help you out with it:
http://developer.android.com/training/basics/fragments/fragment-ui.html
Hope my answer helps you in some way.
The Fragment class can be used many ways to achieve a wide variety of results. In its core, it represents a particular operation or interface that is running within a larger Activity. A Fragment is closely tied to the Activity it is in, and can not be used apart from one. Though Fragment defines its own lifecycle, that lifecycle is dependent on its activity: if the activity is stopped, no fragments inside of it can be started; when the activity is destroyed, all fragments will be destroyed.
MyFragment newFragment = new MyFragment();// MyFragment is a Fragment class
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fra,newFragment, tag);
transaction.addToBackStack(null);
transaction.commit();
sample code
in sample code change
ft.add(android.R.id.content,fragTwo, "tag");
to
ft.add(R.id.fra,fragTwo, "tag");
and add some code in detail.java
public void onStart() {
// TODO Auto-generated method stub
tv.setText(data);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
fm.beginTransaction();
Fragment fragTwo = new MyFragment();
//String tag = getActivity().GetFragmentID();
Fragment f= fm.findFragmentById(getId());
ft.replace(R.id.fra,fragTwo, "tag");
ft.hide(f);
ft.commit();
}
});
super.onStart();
}

fragment placehoder in layout

Suppose I have several fragments:
public class FirstFragment extends Fragment { ... }
public class SecondFragment extends Fragment { ... }
public class ThirdFragment extends Fragment { ... }
I have also a main activity, its content is (main.xml):
<LinearLayout ...>
<!-- How to have a fragment placehold without specify which fragment show here? -->
<fragment>
</LinearLayout>
My question is, in above layout file, how can I define a fragment placeholder without specify which fragment show here, and then in My Activity java code, inflate a proper fragment to the placeholder ?
Is it possible and how to do it?
It is possible.
In your main.xml, define a place holder for the fragment, let's call it "fragment_placeholder":
<LinearLayout ...>
<FrameLayout android:id="#+id/fragment_placeholder" android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>
In your activity, whenever you want to load your fragment:
YourFragment fragment = new YourFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_placeholder, fragment);
ft.commit();
You can also use FragmentContainerView. From the documentation:
FragmentContainerView is a customized Layout designed specifically for
Fragments. It extends FrameLayout, so it can reliably handle Fragment
Transactions, and it also has additional features to coordinate with
fragment behavior.
Example (also from the docs):
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.fragment.app.FragmentContainerView>

Categories

Resources