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

时间:2014-11-25 15:23:41

标签: android

我想在我的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);
                    }
                });

使用此代码,我可以在标题栏中看到后退按钮。但是当我添加了反向的方法时,它不会返回到之前的活动...... Plz帮助我......

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

                return;  
            }

3 个答案:

答案 0 :(得分:0)

我认为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);
    }
}

答案 1 :(得分:0)

使用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);
}

答案 2 :(得分:0)

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

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

2.Override 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.在清单android中指定以下属性:parentActivityName =&#34;活动的逻辑父/要发起的活动的名称&#34;在你的SubActivity中

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

    </activity>