I can transition to a Fragment class using the code below, but how do i transition to a FragmentActivity, using the same code.
FragmentActivity activity = new ABC(); // ABC is a FragmentActivity
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.details, activity); // replace errors out
fragmentTransaction.commit();
You can't replace a view or a fragment with a FragmentActivity! A FragmentActivity is just an Activity and has been created for Android Compatibility support.
To summarize, an Activity can contains Fragments which can be dynamically replaced. The only way to navigate between Activities is by intent.
Related
So I have Activity A with Fragment A.1, and I also have Activity B with Fragment B.1.
What I want to ask is, how do I move directly from Fragment A.1 to Fragment B.1?
I know to move from Fragment A.1 to Activity B, is by:
Intent i = new Intent (getActivity (), MainActivity.class);
startActivity (i);
getActivity ().finish();
But how to move straight to Fragment B.1?
Each Activity A and Activity B has a different <FrameLayout> for Fragment replacement
UPDATE 1.0
I've tried my own way and also the way #cewaphi answered with code like this,
In Activity A:
Intent i = new Intent(TransactionDone.this, MainActivity.class);
i.putExtra("immediatelyTransactionToFragment", true);
startActivity(i);
finishAffinity();
In Activity B:
boolean shouldTransitionToFragment = getIntent().getBooleanExtra("immediatelyTransationToFragment", true);
if (shouldTransitionToFragment) {
Fragment fragment = new Wallet();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFrameLayout, fragment);
transaction.commit();
Log.d("DEBUGGING REDIRECT", "Go to Fragment B.1");
}
The log "Go to Fragment B.1" was created but the transaction doesn't work
When using a single activity and e.g. using the navigation component is not an option.
Consider the following:
In your fragment A.1 when starting the activity store a Boolean
i.putExtra("immediatelyTransationToFragment", true);
In activity B
shouldTransitionToFragment = getIntent().getBooleanExtra("immediatelyTransationToFragment");
// after activity was created
if (shouldTransitionToFragment) {
// Execute the transition action as you would when pressing the button
}
Update 2020/09/07
You are trying to transition to a Fragment from your Activity like this:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFrameLayout, fragment);
transaction.commit();
The documentation states that you should first add the fragment to the activity:
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Apparently your transition works when you click your button. Are you doing it the same?
But I assume at that time the activity has already been created.
Try once to add your fragment instead of replace. I don't know how your container is initialised but adding might be the operation you want, I refer to this good answer for clarification.
Also consider to perform to call this transaction after your activity was created.
I have an Activity with ListView, when clicking on a list item opens a new fragment.
Do I need to create a new fragment every time like this?
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,new MyFragment());
Or will be enough to create a fragment once and then use it?
in activity:
MyFragment myFragment = new MyFragment();
......
in onItemClickListener:
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,myFragment);
It depends on your case. In most cases every list item opens a different fragment (with different data). Then you have to make a static newInstance(Data mySerializableData) method in your fragment, use default constructor inside of it, pass data over Fragment arguments See DetailFragment and use fragmentTransaction.replace() in your activity to add this fragment.
When you dont want your fragment to be changed you can create it only once as you say but there is no need of adding it on every item click. So one creation and only one add.
No, you don't need to create it every time. First, instead of using "add", use "replace". If there is no fragment in fragment manager, your fragment will be added, instead it will be replaced. If you use "add", then you could accidentally add more than one fragment.
You should check for the fragment in the fragment manager and call methods for content updates.
Example:
myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment);
if (myFragment == null) {
myFragment = MyFragment.getInstance(someDataIfNeeded);
fragmentManager.beginTransaction().replace(R.id.my_fragment, myFragment).commit();
} else {
myFragment.updateFragmentContent(someData);
}
check instance of that fragment everytime like this-
In your fragment class -
public static final MyFragment newInstance()
{
MyFragment f = new MyFragment();
return f;
}
and in your activity when you want to create fragment -
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,MyFragment.newInstance());
this is well n good manner...
I have a tabbed view which I have implemented using Action Bar tabs, now there are one or two pages which navigate away from this tab view. At some point of time I want to call one of the fragments in the foreground again. But I am not finding any example of how to do this.
Class Definition(Fragment1_2):
public class Fragment1_2 extends Fragment {
Class Definition(AdhocEdit.class):
public class AdhocEdit extends Activity{
Activity to Fragment Intent so far:
Fragment1_2 fragmentB = (Fragment1_2)getFragmentManager().findFragmentById(R.id.fragemnt1_2);
/* Intent mainIntent;
mainIntent = new Intent(AdhocEdit.this,Fragment1_2.class);
AdhocEdit.this.startActivity(mainIntent);
AdhocEdit.this.finish(); */
// startActivity(new Intent(AdhocEdit.this, Fragment1_2.class));
Commented because none of them works. Also do I need to add this Fragment1_2 into the Manifest, if so how?
You can't start a fragment like you do for activity.
Fragment is hosted by a activity. You need to add the fragment to the container.
Example from docs
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
fragment_container is the id of the container which is usually a FrameLayout and you add the desired fragment the container
More info #
http://developer.android.com/guide/components/fragments.html
I would like to add a FragmentActivity in the activity layout. In order to make fragment transactions (such as add, remove, or replace a fragment), the api guides say that I first need to get an instance of FragmentTransaction from your Activity and then add a fragment using the add() method specifying the fragment to add and the view in which to insert it. Ok pretty straightforward so far, but what should I do in the FragmentActivity case?
AllEventsFragments events;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if ( savedInstanceState == null )
{
events = new AllEventsFragments();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.content_frame, events,"events");
// Commit the transaction
transaction.commit();
}
}
in which AllEventsFragments is defined as follows:
public class AllEventsFragments extends FragmentActivity implements ActionBar.TabListener
{
...
}
Since the add method accepts a Fragment as second argument the error returned is:
The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, AllEventsFragments, String)
I would like to add a FragmentActivity in the activity layout.
You are trying to nest activities. That is not supported via fragment transactions, and what little support there ever was for it has been deprecated for ~2.5 years.
However, you can move much of the AllEventsFragments logic into a Fragment, which can then be used from both AllEventsFragments and wherever else you are trying to use it.
The main fragment activity in my application has the following function
private final void insertFragmentIntoView(final SherlockFragment fragment,
String tag) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, fragment, tag);
ft.commit();
}
The R.id.fragment_content is a frame layout and i basically insert a new fragment into this frame. Now the frame that i insert has a button that should take me onto a new screen. What i want is that all navaigation in my application should take place in my FragmentActivity. How can i call my FragmentActivity from with a child fragment ?
Kind Regards
Make a listener class in your fragment and your parent activity will implement that listener .
Now register listener in your fragment and call method in which you want to perform some action.
More you can see this link http://developer.android.com/training/basics/fragments/communicating.html
Example how fragment and activity communicate.
class MyFragment extends Fragment{
class interface MyFragmentListener {
doSomeAction();
}
MyFragmentListener myListener;
onAttach(){
myListener=(MainActivity )getActivity();
}
onButtonClick(){
myListener.doSomeAction();
}
}
class MainActivity extends FragmentActivity implements MyFragment.MyFragmentListener
{
doSomeAction(){
//TODO perform some action from your fragment to activity
}
}
Using listeners is the recommended way of communicating between Fragment and your activity.
See this Android documentation section for infromation. Long story short they just implement a listener interface by the Activity class and cast getActivity() result in a fragment to a listener.
From my personal experience this is very convenient because lets you to:
Easilly switch underlying activity (e.g. you host entire fragment in a wrapper activity for compatibility in pre-3.0 and host this fragment along with others in 11+)
Easilly control if the wrapper activity supports callbacks or not. Just check is it does implement the listener and do your app specific actions if it doesn't.