2015-04-03 56 views
0

我想做什么如何解除由另一活动显示的对话框片段?

我有一个对话框片段显示从活动A包含一个按钮。当按下按钮时,活动B启动。在活动B中,用户所做的任何动作都会导致活动B通过调用finish()结束,但是我希望在调用finish()之前将Activity中的Dialog片段关闭,以便当用户返回活动A时,这个对话框不再显示。

问题

正如你在代码段看到,我已经重新在一个小规模的问题,以避免张贴的代码巨额以及简化的问题。 问题是我所拥有的应用程序中包含了很多可以显示对话框片段的活动,所以我不知道如何跟踪启动对话框片段的活动。但是我知道另一个活动找不到从另一个活动添加的片段,所以我想知道是否有人知道解决此问题的方法。

代码

对话框片段

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; 
    } 

活性A - 其中显示的对话框片段上的按钮按压

public void showDialogFragment(View view) 
{ 
    MyDialogFragment.newInstance().show(getSupportFragmentManager(),MyDialogFragment.TAG); 
} 

活动B - 这一个尝试通过一个按钮来关闭从活动一对话框单击

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 :("); 
    } 
} 

我有什么累

下面是我试图做,没有工作。我的想法是,如果使用全局片段管理器来显示片段,并且该管理器应该能够找到它。

我所做的是创建一个活动A和B扩展的基本活动。

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; 
    } 


} 

现在活动A和B用它来展示并试图关闭该对话框片段

在活动A

MyDialogFragment.newInstance().show(getGlobalFragmentManager(),MyDialogFragment.TAG); 

在活动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 :("); 
     } 
    } 

这仍然导致在对话框中为空并且不能解除。

回答

2

据我了解你的问题,主要问题是你在活动A中开始一个对话框,然后移动到活动B,当你回到A时,如果这是场景,你的对话框仍然存在,那么在活动A

@override 
    protected void onPause(){ 
//write code to dismiss dialog 
    } 

无论何时新的活动开始,旧的一个去onPause()(如果还没有完成)。

+0

谢谢,这使我指出了正确的方向。我所做的是扩展Application类来存储全局变量。然后,一旦Activity B完成,我就将布尔值设置为true。活动B完成后,会调用对话框片段的onResume,它将检查此Booelan的值。如果属实,我只是打电话解雇。 – 2015-04-04 11:28:20

相关问题