Activity Passing Data to Fragment through callbacks - android

I got a situation here .
I have FragmentActivty which holds a Fragment . when i click on a button in Fragment i am going to a Activty . when i reach the activity i do something which will affect the data displayed in Fragment where i came from. So in order to bring that changes in fragment i would like to give a callback from the Activity to Fragment. First i thought of implementing onActivityResult. But i realized it's not what i needed.
Is my approach is wrong?? Please guide me
MyActivity extends FragmentActivity
MyActivity holds
MyFragment extends Fragment
From here i'm going to
SecondActivity extends Activity
from SecondActivity i need to get a callback to MyFragment . Is there anything wrong with my approach ??
EDIT:
public class MainActivity extends FragmentActivity {
private FrameLayout frameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.framelayout);
loadFragment();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void loadFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.framelayout, myFragment);
fragmentTransaction.commit();
}
}
MyFragment.java
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
callActivity();
}
});
return view;
}
private void callActivity() {
Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivityForResult(intent, 10);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
Log.e("MyFragment Inside", "Onresultttt");
if (requestCode == 10) {
if (resultCode == Activity.RESULT_OK) {
Log.e("Result code", Activity.RESULT_OK + " okkk");
}
if (resultCode == Activity.RESULT_CANCELED) {
Log.e("Result code", Activity.RESULT_CANCELED + "cancelll inside fragment");
}
}
}
}
SecondActivity.java
public class SecondActivity extends Activity {
private static final String TAG = "second activity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity_view);
}
#Override
public void onBackPressed() {
// super.onBackPressed();
Intent intent = new Intent();
setResult(RESULT_CANCELED, intent);
Log.e(TAG, "result setted");
finish();
}
}

Refer this question it will help you understand what is happening:
onActivityResult is not being called in Fragment

If I understand your situation correctly, then the correct way of handling the communication is to have SecondActivity pass back information to MyActivity, which will in turn configure it's instance of MyFragment directly. You don't want to be accessing Fragments from Activities unless the Fragment is attached to the Activity.
As for how to do the communication, as you suggested, one way of doing it would be through the use of startActivityForResult(). See this answer for more details: How to manage `startActivityForResult` on Android?
Just a note about startActivityForResult(). If you are calling it from the Fragment then your Fragment will receive the result, not your Activity. There are also some other issues with calling startActivityForResult() from a Fragment, so I would generally recommend that you instead call it from the Activity and therefore handle the result from the Activity.
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, INTENT_CODE);
The best practice way of communicating from a Fragment to an Activity is by defining an Interface in the Fragment, which the Activity implements.
public class MyFragment extends Fragment {
public interface MyFragmentListener() {
public void onMyFragmentEvent();
}
public void startTheActivityForResult() {
((MyFragmentListener)getActivity()).onMyFragmentEvent();
}
}
public class MainActivity extends FragmentActivity implements MyFragmentListener{
#Override public void onMyFragmentEvent() {
}
}
The Fragment then simply casts the reference to the Activity it is attached to, knowing that the Activity must implement the Listener, thus allowing you to reuse the Fragment in other Activities.

Related

Transition between fragments via back button and home button

I have a fragment with a view and an options menu:
public class OfferingsFragment extends Fragment
{
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.offering_tiles, null);
...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent = new Intent(getActivity(), SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
From the options menu, the user opens this preference fragment, which is hosted by the SettingsActivity:
public class SettingsActivity extends Activity {
private SettingsFragment settingsFragment = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settingsFragment = new SettingsFragment();
getFragmentManager().beginTransaction()
.replace(android.R.id.content, settingsFragment)
.commit();
}
The view of the OfferingsFragment depends on one of the preferences. That is, after this preference has changed, the OfferingsFragment must be refreshed by calling onCreateView again. What I do is this:
Open preference screen from OfferingsFragment's option menu
Change preference
Return to OfferingsFragment
If I return to the OfferingsFragment via the Home Button (left arrow in ActionBar), then the OfferingsFragment gets refreshed by calling its onCreateView (which is the desired effect). However, if I return to the OfferingsFragment via the Back Button (on the device), onCreateView is NOT CALLED and thus the view is NOT re-created. What I want is that the view is also re-created when the user presses the Back Button. Any ideas how to achieve this?
What Happens
When you press Up button parent activity is called via startActivity which means a new instance is created by default.
When you press Back button current activity is finished and you're back in the previous activity and its already existing instance (it was in stopped state).
How To Deal With It
What I want is that the view is also re-created when the user presses the Back Button. Any ideas how to achieve this?
Start the settings activity via startActivityForResult:
public static final int RC_SETTINGS = 1;
private void startSettingsActivity() {
Intent i = new Intent(this, SettingsActivity.class);
startActivityForResult(i, RC_SETTINGS);
}
When the result comes back reattach the fragment. This will recreate its view hierarchy.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Call to super if you value your life. And want proper lifecycle handling.
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SETTINGS) {
// If we just came back from SettingsActivity...
// ...reattach fragment and trigger view recreation.
final FragmentManager fm = getFragmentManager();
final f = fm.findFragmentById(android.R.id.content);
fm.beginTransaction().detach(f).attach(f).commit();
}
}
Replace the fragment ID with whatever you used.
Pro tip
If your fragment is not misconfigured you should be able to call
public class SettingsActivity extends Activity {
private SettingsFragment settingsFragment = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
settingsFragment = new SettingsFragment();
getFragmentManager().beginTransaction()
.replace(android.R.id.content, settingsFragment)
.commit();
} else {
settingsFragment = (SettingsFragment) getFragmentManager().findFragmentById(android.R.id.content);
}
}
}
This is both resourceful and practical as your original code would lose state (for example scroll position) on configuration change.

How we get the previous data of fragment in android?

In my application Main_Activity contain four fragments. In one fragment (called mobile_fragment) whenever i click the button in fragment it will move to another activity, in that i need to get the one value and need to send that data to that fragment(mobile_fragment).
what i done is:
whenever i click the button in fragment i moved to activity and get the value and that value sent to main_Activity, after that in mobile fragment i accessed that variable (static declaration of variable in main_activity).
Now my problem is after getting the value in mobile_fragment, i already entered values in remaining edit text widgets are cleared (no text). how i get previously entered values?
please any one help me.
fragment
public class Mobile_Fragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (container == null) {
return null;
}
browseplans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), BrowsePlans_Activity.class);
startActivity(intent);
}}
BrowseplansActivty:
Intent intent=new Intent(BrowsePlans_Activity.this,MainActivity.class);
intent.putExtra("Amount",amount);
startActivity(intent);
Main_Activity:
public class MainActivity extends FragmentActivity implements OnClickListener {
public static String TAG = "Main First Frag";
public static String contact_number, prepaid_amt;
Fragment fragment;
FragmentManager frgManager;
FragmentTransaction ft;
Bundle b = new Bundle();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initUI();
bindEvents();// now this is my changed one
Intent in=getIntent();
prepaid_amt=in.getStringExtra("Amount");
Log.v("TAG+AMOUNT",""+prepaid_amt);
}
}
To pass data from your fragment to the Activity you are starting, pass it through Intent Extras.
To return data from the Activity back to the fragment, call setResult with a result code and an intent that, again, holds data set with extras. Retrieve that data in the onActivityResult override of your fragment.
To save information in a fragment when it is detached and then re-attached, use the override onSaveInstanceState and then extract data from that state in onCreate and/or onCreateView overrides.

Resuming a Tic-tac-toe game in Android after closing the fragment

I'm learning Android development by creating a test project: Tic-tac-toe. My Tic-tac-toe app starts with a Main Menu activity with a Button that says New Game. After clicking New Game, an activity with a fragment containing a Tic-Tac-Toe game (in a GridLayout) launches. The user can play the game, but when they go back to the Main Menu, the game state is not saved.
I want to change this so that when the user goes back to the Main Menu, they will see a new Button called "Continue" (in addition to the "New Game" Button). Once the user clicks "Continue", the game they were playing before continues. If the click the "New Game" button, a new game will be launched like before.
By using the onSaveInstanceState method and a Bundle savedInstanceState, I was able to preserve the game state on orientation changes (by saving the data from the underlying TicTacToe class). My code below shows how I did that. I would like to do something similar this time again - I have read about it, but I don't quite understand the best way to start. Can anyone show me the right steps?
Note that I'm doing this programmatically (i.e. designing the layout outside the xml files)!
My MainMenu class:
public class MainMenu extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void startGame(View view) {
Intent intent = new Intent(this,MainGame.class);
startActivity(intent);
}
}
with a corresponding xml file activity_main_game:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/new_game"
android:onClick="startGame"
/>
</RelativeLayout>
Now my MainGame.class looks like this:
public class MainGame extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_game);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new BoardFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_game, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class BoardFragment extends Fragment {
public TicTacToe t = new TicTacToe(); //A class I wrote that launches a simple TicTacToe game
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
t = new TicTacToe(savedInstanceState.getInt("TicTacToeData"),
);
}
//Graphics stuff here: variable rootView which contains the TicTacToe grid is defined
//and onClickListeners are added to the ImageViews in the GridLayout which makes corresponding
//changes to t.
return rootView;
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("TicTacToeData", t.getGameData());
}
}
}
Thanks for any help.
You have two options
SharedPreferences: you can use shaerdPreferences to store the state of the game. It would be stored also after the restart of the application.
start your game activity with startActivityForResult() and implementing onActivityResult on your parent activity.
There are 2 things you have to do:
Get notified within Main class when your MainGame Activity is closed
Update the UI to change the button text
To get notified within Main class:
You can start MainGame Activity using startActivityForResult instead of startActivity. Then override the onActivityResult method on Main class to be notified when the user close the MainGame.
Also you will have to modify MainGame to invoke setResult(int resultCode, Intent data) or setResult(int resultCode) method when the user click the back button (depending on whether you want to pass additional data back to the Activity.
Override the onBackPressed() method in your MainGame class to invoke the setResult method. E.g.
public void onBackPressed() {
setResult(RESULT_OK);
super.onBackPressed(); // otherwise your Activity won't be closed
}
Have a look at http://developer.android.com/reference/android/app/Activity.html#StartingActivities for a more detailed example
To Update the UI:
Save the button as a local variable (in your onCreate method)
Update the Button text MainGame Activity
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// Update the button text to "Continue"
} else {
// Change the button text back to "New Game"
}
}
}
One possible solution is to have your MainGame Activity return the state of your game when it finishes. Then your MainMenu Activity can save that value and use it to start up a game in the same state.
For starters, you will want to start your game Activity in a way that enables it to return a value:
public class MainMenu extends ActionBarActivity {
...
public void startGame() {
Intent intent = new Intent(this, MainGame.class);
startActivityForResult(intent, 1);
}
}
Next you will want to make sure that your board Fragment returns the correct state whenever it is closed:
public class BoardFragment extends Fragment {
...
#Override
public void onPause() {
Intent result = new Intent();
result.putExtra("TicTacToeData", t.getGameData());
getActivity().setResult(Activity.RESULT_OK, result);
}
}
Afterwards you will want your menu Activity record the result:
public class MainMenu extends ActionBarActivity {
private int mPreviousGameState;
...
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
mPreviousGameState = 1;
}
}
}
Now you can pass in this state when you start your game Activity:
public class MainMenu extends ActionBarActivity {
...
public void continueGame() {
Intent intent = new Intent(this, MainGame.class);
intent.putExtra("TicTacToeData", mPreviousGameState);
startActivityForResult(intent, 1);
}
}
Now you will need a way to create a board that is already in a specific game state:
public class BoardFragment extends Fragment {
public TicTacToe t = null
...
public BoardFragment (int gameState) {
t = new TicTacToe(gameState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
// recreate the board after an orientation change
t = new TicTacToe(savedInstanceState.getInt("TicTacToeData"));
} else if (t == null) {
// create a new board if this is a new game
// if this is a continued game, the board is already setup
t = new TicTacToe();
}
}
And lastly you will need to call the correct version of BoardFragment from your game:
public class MainGame extends ActionBarActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_game);
if (savedInstanceState == null) {
Intent intent = getIntent();
int gameState = intent.getIntExtra("TicTacToeData", -1);
if (gameState == -1) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new BoardFragment()).commit();
} else {
getSupportFragmentManager().beginTransaction().add(R.id.container, new BoardFragment(gameState)).commit();
}
}
}
}
Another solution is to redesign your app to have only a single Activity (or ActionBarActivity) subclass and multiple Fragment subclasses for each screen you want to show. You can keep a reference to both fragments in your Activity. This will implicitly retain the state of the fragment which displays the game board.

Android: Passing data from child fragment to parent fragment

I need to pass some data from the child fragment to the parent fragment that I will be able to read when I go back to the parent fragment. In detail:
I have a FragmentActivity that calls FragmentParent. From FragmentParent I call FragmentChild like this:
FragmentChild fragmentChild = new FragmentChild();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frl_view_container, fragmentChild);
transaction.addToBackStack(null);
ctransaction.commit();
In FragmentChild I set a string value which I need to pass back to FragmentParent and then I return back to FragmentParent.
String result = "OK";
getFragmentManager().popBackStack();
What is the best/proper way to read the result string in FragmentParent?
Android architecture components solution:
In case you are using Android architecture components, it possible to share data between all Fragments of an Activity with a ViewModel. Ensure ViewModelProviders makes use of Activity context to create ViewModels.
public class SharedViewModel extends ViewModel {
private final MutableLiveData<Item> selected = new MutableLiveData<Item>();
public void select(Item item) {
selected.setValue(item);
}
public LiveData<Item> getSelected() {
return selected;
}
}
public class MasterFragment extends Fragment {
private SharedViewModel model;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
itemSelector.setOnClickListener(item -> {
model.select(item);
});
}
}
public class DetailFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
model.getSelected().observe(this, { item ->
// Update the UI.
});
}
}
Non Android architecture components solution:
You can use setTargetFragment and onActivityResult to achieve this.
Set FragmentParent instance as target fragment on FragmentChild instance i.e.
FragmentChild fragmentChild = new FragmentChild();
fragmentChild.setTargetFragment(this, FRAGMENT_CODE);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frl_view_container, fragmentChild);
transaction.addToBackStack(null);
transaction.commit();
In FragmentChild, wherever you are invoking the popBackStack, call onActivityResult on the set target Fragment. Use Bundle to pass on additional data.
Intent intent = new Intent();
intent.putExtra(FRAGMENT_KEY, "Ok");
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
getFragmentManager().popBackStack();
Back in FragmentParent, override the default onActivityResult method.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == FRAGMENT_CODE && resultCode == Activity.RESULT_OK) {
if(data != null) {
String value = data.getStringExtra(FRAGMENT_KEY);
if(value != null) {
Log.v(TAG, "Data passed from Child fragment = " + value);
}
}
}
}

Switching Fragments in Master/Detail Flow

I am attempting to create an app which has a Master/Detail flow using Fragments. Selecting an item will open a detail fragment which may then which to "open" another fragment and add it to the back stack.
I have renamed classes to help illustrate what they do.
public class ListOfDetails extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
//Callback method indicating that an item with the given ID was selected.
public void onItemSelected(String id) {
// Performing logic to determine what fragment to start omitted
if (ifTwoPanes()) {
Fragment fragment = new DetailFragmentType1();
getSupportFragmentManager().beginTransaction().replace(R.id.aContainer, fragment).commit();
} else {
Intent newIntent = new Intent(this, SinglePaneFragmentWrapper.class);
newIntent.putExtra("id", id);
startActivity(newIntent);
}
}
// My attempt at making it possible to change displayed fragment from within fragments
public void changeDetailFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.replace(R.id.aContainer, fragment);
transaction.commit();
}
}
An example of one of the detail fragments. There are many different Fragments that may be created in different circumstances.
public class DetailFragmentType1 extends Fragment {
private ListOfDetails parent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Activity a = getActivity();
if (a instanceof ListOfDetails) {
parent = (ListOfDetails) a;
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button aButton = (Button) getActivity().findViewById(R.id.aButton);
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
parent.changeDetailFragment(new SubDetailFragment());
}
});
}
}
When on phone, a wrapper activity is used to hold the fragment
public class SinglePaneFragmentWrapper extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Duplicate logic must be performed to start fragment
// Performing logic to determine what fragment to start omitted
String id = getIntent().getStringExtra("id");
if(id == "DetailFragmentType1") {
Fragment fragment = new DetailFragmentType1();
getSupportFragmentManager().beginTransaction().replace(R.id.aContainer, fragment).commit();
} else {
...
}
}
}
What is the proper way to change the fragment that is open in the detail pane in this circumstance? My method feels like a hack when using two panes and doesn't even work when using only one pane because getParent() from SinglePaneFragmentWrapper returns null, making me unable to call parent.changeDetailFragment().
This is a complicated question, hopefully I explained it well. Let me know if I missed something. Thanks
There are lots of opinions around this and lots of ways of doing it. I think in this case the problem is "who is responsible for changing the fragment?" on the surface it seems that a listener on the button is the obvious place, but then the fragment shouldn't know what it is hosted in (a symptom of that is getting an undesirable result like null from getParent()).
In your case I would suggest you implement a "listener" interface in the parent and "notify" from the fragment.. when the parent is notified, it changes the fragment. This way the fragment is not changing itself (so doesn't need to know how).. so.. for your case..
Add a new interface:
public interface FragmentChangeListener {
void onFragmentChangeRequested(Fragment newFragment);
}
Implement the interface in your ListOfDetails activity
public class ListOfDetails extends FragmentActivity implements FragmentChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
//Callback method indicating that an item with the given ID was selected.
public void onItemSelected(String id) {
// Performing logic to determine what fragment to start omitted
if (ifTwoPanes()) {
Fragment fragment = new DetailFragmentType1();
getSupportFragmentManager().beginTransaction().replace(R.id.aContainer, fragment).commit();
} else {
Intent newIntent = new Intent(this, SinglePaneFragmentWrapper.class);
newIntent.putExtra("id", id);
startActivity(newIntent);
}
}
// My attempt at making it possible to change displayed fragment from within fragments
public void changeDetailFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.replace(R.id.aContainer, fragment);
transaction.commit();
}
// This is the interface implementation that will be called by your fragments
void onFragmentChangeRequested(Fragment newFragment) {
changeDetailFragment(newFragment);
}
}
Added listener to detail fragment
public class DetailFragmentType1 extends Fragment {
private FragmentChangeListener fragmentChangeListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Actually you might not have an activity here.. you should probably be
// doing this in onAttach
//Activity a = getActivity();
//if (a instanceof ListOfDetails) {
// parent = (ListOfDetails) a;
//}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button aButton = (Button) getActivity().findViewById(R.id.aButton);
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// parent.changeDetailFragment(new SubDetailFragment());
notifyFragmentChange(new SubDetailFragment());
}
});
}
#Override
public void onAttach(Activity activity) {
// This is called when the fragment is attached to an activity..
if (activity instanceof FragmentChangeListener) {
fragmentChangeListener = (FragmentChangeListener) activity;
} else {
// Find your bugs early by making them clear when you can...
if (BuildConfig.DEBUG) {
throw new IllegalArgumentException("Fragment hosts must implement FragmentChangeListener");
}
}
}
private void notifyFragmentChange(Fragment newFragment) {
FragmentChangeListener listener = fragmentChangeListener;
if (listener != null) {
listener.onFragmentChangeRequested(newFragment);
}
}
}
And implement the same interface to your single pane activity...
public class SinglePaneFragmentWrapper extends FragmentActivity implements FragmentChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Duplicate logic must be performed to start fragment
// Performing logic to determine what fragment to start omitted
String id = getIntent().getStringExtra("id");
if(id == "DetailFragmentType1") {
Fragment fragment = new DetailFragmentType1();
getSupportFragmentManager().beginTransaction().replace(R.id.aContainer, fragment).commit();
} else {
...
}
}
// My attempt at making it possible to change displayed fragment from within fragments
public void changeDetailFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.replace(R.id.aContainer, fragment);
transaction.commit();
}
// This is the interface implementation that will be called by your fragments
void onFragmentChangeRequested(Fragment newFragment) {
changeDetailFragment(newFragment);
}
}
Note the similarity between your single pane and your multi-pane activities.. this suggests that you could either put all of the duplicated code (changefragment etc) into a single activity that they both extend or that in maybe they are the same activities with different layouts...
I hope that helps, Good luck.
Regards,
CJ

Categories

Resources