硬件后退按钮和导航栏后退按钮之间的差异 [英] Differences between hardware back button and navigation bar back button

查看:103
本文介绍了硬件后退按钮和导航栏后退按钮之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的编程,有硬件后退按钮,并在导航栏上的后退按钮之间的差异。在我的应用程序,有一种观点在那里,如果我点击硬件后退按钮,应用程序崩溃。但是,如果我点击导航栏上的按钮,它工作完全正常。不同的方法的基础上的两种不同类型的按钮(如在OnCreate VS onresume)。

In Android programming, is there a difference between the hardware back button and the back button in the navigation bar. In my application, there is a view where if I click the hardware back button, the app crashes. But, if I click the button in the navigation bar it works perfectly fine. Are different methods called based on the two different types of buttons (such as oncreate vs onresume).

类我叫后退按钮的:

public class ViewContact extends ActionBarActivity {
    Button btnDelete;
    TextView name;
    TextView position;
    TextView email;
    TextView phone;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_contact);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    name = (TextView)findViewById(R.id.contactName);
    position = (TextView)findViewById(R.id.contactPosition);
    email = (TextView)findViewById(R.id.contactEmail);
    phone = (TextView)findViewById(R.id.contactPhone);
    btnDelete = (Button)findViewById(R.id.deleteContactBtn);

    Bundle takeBundledData = getIntent().getExtras();

    final String contactName = takeBundledData.getString("clickedName");
    String contactPosition = takeBundledData.getString("clickedPosition");
    String contactEmail = takeBundledData.getString("clickedEmail");
    String contactPhone = takeBundledData.getString("clickedPhone");

    name.setText(contactName);
    position.setText(contactPosition);
    email.setText(contactEmail);
    phone.setText(contactPhone);

    btnDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MySQLiteHelper androidOpenDbHelper = new MySQLiteHelper(getApplicationContext());
            SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();

            String[] whereClauseArgument = new String[1];
            whereClauseArgument[0] = contactName;

            // Only difference between UPDATE and DELETE is
            //DELETE does not have ContentValues part
            sqliteDatabase.delete(MySQLiteHelper.TABLE_NAME_WIL, MySQLiteHelper.COLUMN_NAME_NAME + "=?", whereClauseArgument);

            sqliteDatabase.close();
            finishActivity(0);
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.view_contact, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

logcat的错误:

Logcat error:

java.lang.RuntimeException: Unable to resume activity: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@43cfb6f0

除了误差,虽然这是一个不同的问题,为何只有在硬件发生后面,而不是导航栏中的后退按钮

Aside from the error though which is a different issue, why does it only happen with the hardware back and not the navigation bar back button

推荐答案

在preSS的'硬件后退按钮或提供的软后退按钮就会触发的 onBack pressed()方法在活动。如你愿意,你可以重写此行为。

When you press the 'hardware back button' or the 'provided soft back button' it will trigger the onBackPressed() method in the Activity. You can override this behavior as you wish.

这是它的导航栏后退按钮不同的是,在这一个你必须自己处理,就像你正在做的 onOptionsItemSelected()方式上的'android.R .id.home情况。

The difference from it to the Navigation Bar 'Back Button' is that on this one you have to handle yourself, like you're doing on the onOptionsItemSelected() method on the 'android.R.id.home' case.

您遇到的问题可能是因为'previous'活动是滥用了光标。当你preSS回到了ViewContact',这将完成previous活动的ViewContact活动,并在 onResume()将会再次调用。因此,当出现这种情况,显然你的previous活动正在试图重新使用一个封闭的游标,那么错误发生。在您的 NavUtils.navigateUpFromSameTask(本)你可能在preventing这种情况的发生。但官方并默认行为是,我所描述的。

The problem you're having is probably because the 'previous' activity is misusing a 'Cursor'. When you press back on the 'ViewContact', it will finish the 'ViewContact' activity and the onResume() of the previous activity will be called again. So when this happens, apparently your previous activity is trying to reused a closed cursor, then the error happens. On your NavUtils.navigateUpFromSameTask(this) you are probably preventing this from happening. BUT the OFFICIAL and DEFAULT behavior is the one that i described.

这篇关于硬件后退按钮和导航栏后退按钮之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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