仅从MainActivity关闭应用程序,而不是从子片段/活动关闭

时间:2014-04-08 06:14:35

标签: android android-fragments android-fragmentactivity

我想关闭背面的应用程序。 例如,我有3个片段

  1. 扩展FragmentActivity的MainActivity
  2. FragmentTwo,它扩展了Fragment
  3. FragmentThree,它扩展了Fragment
  4. 所以当我在第3个片段和后退按钮时按下第2个片段。但是,当我在第一个片段并按下后退按钮时,应用程序以Toast / AlertDialog消息关闭。

2 个答案:

答案 0 :(得分:0)

使用类似的东西

public void onBackPressed() 
{
    new AlertDialog.Builder(this)
    .setTitle("")
    .setMessage("Are you sure?")
    .setNegativeButton(android.R.string.no, null)
    .setPositiveButton("ok",new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int whichButton) 
        {
               Intent i=new Intent(this,xxxx.class);
               startActivity(i);
               dis.super.onBackPressed();

        }
    }).create().show();
}

答案 1 :(得分:0)

请试试这个:

@Override  
public void onBackPressed()
{
      if (doubleBackToExitPressedOnce) {          
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this,"click again to finish", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;                       
            }
        }, 2000);
}
相关问题