繁体   English   中英

使用标题栏中的“后退”按钮导航到上一个活动

[英]Navigate to previous activity with back button in title bar

我想在我的Android应用程序中添加后退按钮。为此在子活动中我添加了此代码

   setContentView(R.layout.activity_location);
     getActionBar().setDisplayHomeAsUpEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        // getting values from selected ListItem
                        String FC_DATE = ((TextView) view.findViewById(R.id.fc_date)).getText()
                                .toString();

                        // Starting new intent
                        Intent in = new Intent(getApplicationContext(),
                                ForecastActivity.class);
                        // sending lat/long to next activity

                        in.putExtra(TAG_FC_DATE, FC_DATE);
                        in.putExtra(TAG_LAT, LAT);
                        in.putExtra(TAG_LONG, LONGITUDE);
                        // starting new activity and expecting some response back
                        startActivityForResult(in, 100);
                    }
                });

使用此代码,我无法看到标题栏中的后退按钮。 但是当我添加了backpressed方法时,它将不会返回到先前的活动...请帮助我...

@Override
        public void onBackPressed() 
            {
             this.startActivity(new Intent(LocationActivity.this,AndroidGPSTrackingActivity.class));  

                return;  
            }

我认为onBackPressed是指手机本身的按钮。

通过ActionBar,您应该使用此侦听器

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case putYourIdHere:
        startActivity(new Intent(LocationActivity.this,AndroidGPSTrackingActivity.class));  
        return true;  
    default:
        return super.onOptionsItemSelected(item);
    }
}

使用onOptionsItemSelected方法捕获向上按钮:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    int id = item.getItemId();
    if (id == android.R.id.home)
    {
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown.        
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

1.在onCreate()方法中,添加

        getActionBar().setDisplayShowHomeEnabled(true);
        getActionBar().setHomeAsUpIndicator(R.drawable.ic_launcher); //custom app icon
        getActionBar().setDisplayHomeAsUpEnabled(true);

2.重写onOptionsItemSelected(MenuItem item)

        int id = item.getItemId();
        switch(id){
        case R.id.home:
            finish();
            return true;

        //handle other actions
        case R.id.action_settings:
            return true;
        }
        return super.onOptionsItemSelected(item);

3.在SubActivity中的清单android:parentActivityName =“活动的逻辑父级/要启动的活动名称中指定以下属性”

    <activity
        android:name=".SubActivity"
        android:label="SubActivity"
        android:parentActivityName="com.example.actionbardemo.MainActivity"
         >

    </activity>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM