如何使用onBack $ P $工作pssed()在Android中? [英] How to work with onBackPressed() in Android?

查看:222
本文介绍了如何使用onBack $ P $工作pssed()在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动A,活动'B',活动'C',活动'D',活动'E'活动'E'。我想退出的onBack pressed活动点击()。当我运行它去到活动'A'的应用程序不会退出该应用程序。我怎样才能用这个工具。

I have Activity 'A', Activity 'B', Activity 'C', Activity 'D', Activity 'E', Activity 'E' . I want to exit the Activity click on onBackPressed(). When i run the app it goes to Activity 'A' it does not exit the app. How can i implement with this.

下面是我的code

public class MainActivity extends Activity {

    TableLayout table_layout;
    EditText firstname_et, lastname_et;
    Button addmem_btn;

    DatabaseHelper databaseHelper = new DatabaseHelper(this);
    protected SQLiteDatabase db;

    ProgressDialog PD;
    ImageView imgButtonBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.performance_report);

        table_layout = (TableLayout) findViewById(R.id.tableLayout_PerformanceReport);


         //ImageView imgButtonBack;
         imgButtonBack = (ImageView)findViewById(R.id.imagBackButton);
         imgButtonBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this, Employee_List.class);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
                //finish();
            }
        });


    }

}
    @Override
    public void onBackPressed()
    {
        AlertDialog.Builder alertbox = new AlertDialog.Builder(MainActivity.this);

        alertbox.setTitle("Do you wish to exit ?");
        alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) { 
               // finish used for destroyed activity
                finish();
            }
        });

        alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                    // Nothing will be happened when clicked on no button 
                    // of Dialog     
          }
        });

        alertbox.show();
   }
}

由于鸭preciate。

Thanks to Appreciate.

推荐答案

您可以试试这个 onBack pressed

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

哪个退出应用程序。对我来说工作正常。希望它帮助。

Which exits the application. And works fine for me. Hope it helps.

这篇关于如何使用onBack $ P $工作pssed()在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆