What I want to do
I have a dialog fragment shown from Activity A which contains a button. When the button is pressed Activity B is started. Within activity B, what ever action the user does causes activity B to end via a call to finish() however I wish to dismiss the Dialog fragment from Activity B before the call to finish() so that when the user navigates back to activity A, this dialog is no longer shown.
The problem
As you see in the code section, I have recreated the problem on a small scale to avoid posting huge amounts of code and to simplify the question. The issue is that the application I have contains a lot of activities which can show the dialog fragment so I do not know how to keep track of the activity which started the dialog fragment. However I know that another activity cannot find fragments added from another so I was wondering if there is someone who knows a way around this problem.
The code
Dialog Fragment
public class MyDialogFragment extends DialogFragment {
public static final String TAG = "MyDialogFragment";
public static MyDialogFragment newInstance(){
MyDialogFragment myDialogFragment = new MyDialogFragment();
return myDialogFragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.mydialogfragment,container,false);
Button goToSomeOtherActivity = (Button)view.findViewById(R.id.goToSomeOtherActivity);
goToSomeOtherActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),SomeOtherActivity.class);
startActivity(intent);
}
});
return view;
}
Activity A - which displays the dialog fragment on button press
public void showDialogFragment(View view)
{
MyDialogFragment.newInstance().show(getSupportFragmentManager(),MyDialogFragment.TAG);
}
Activity B - This one attempts to close the dialog from activity A via a button click
public void dismissDialogFragment(View view)
{
MyDialogFragment myDialogFragment = (MyDialogFragment) getSupportFragmentManager().findFragmentByTag(MyDialogFragment.TAG);
if(myDialogFragment != null)
{
Log.d("SomeOtherActivity","Its not null, we got it");
}
else{
Log.d("SomeOtherActivity","it was null :(");
}
}
What I have tired
Below is what I have tried to do which did not work. I had the idea that if a global fragment manager was used to show the fragment and this manager should be able to find it.
What I did was create a base activity which Activity A and B extend from.
public class BaseActivity extends ActionBarActivity {
private FragmentManager globalFragmentManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
globalFragmentManager = getSupportFragmentManager();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
}
public FragmentManager getGlobalFragmentManager(){
return globalFragmentManager;
}
}
Now activity A and B used this to show and attempt to dismiss the dialog fragment
In Activity A
MyDialogFragment.newInstance().show(getGlobalFragmentManager(),MyDialogFragment.TAG);
In activity B
public void dismissDialogFragment(View view){
MyDialogFragment myDialogFragment = (MyDialogFragment)getGlobalFragmentManager().findFragmentByTag(MyDialogFragment.TAG);
if(myDialogFragment != null)
{
Log.d("SomeOtherActivity","Its not null, we got it");
}
else{
Log.d("SomeOtherActivity","it was null :(");
}
}
This still resulted in the dialog being null and not able to dismiss.
0 comments:
Post a Comment