簡體   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