簡體   English   中英

Android 自定義標題欄上的主頁按鈕

[英]Android Home button on custom title bar

我在自定義標題欄上構建了主頁按鈕(使用圖片到按鈕)。 我的問題是每次單擊此按鈕。 它將 go 為主。 當停留在主頁並單擊按鈕時。 它將一次又一次地進入主頁。 我怎樣做?? 我希望它不是 go 在保持主要或無法在主頁中單擊此按鈕時變為主要。

明白了嗎?

請幫幫我謝謝

public class CustomTitleBar extends Activity {
protected ImageButton toHome;
protected TextView title;
protected ImageView icon;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.main);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);

    toHome = (ImageButton) findViewById(R.id.header);
    title = (TextView) findViewById(R.id.title);
    icon  = (ImageView) findViewById(R.id.icon);

    ProgressBar titleProgressBar = (ProgressBar) findViewById(R.id.loadProgress);
    titleProgressBar.setVisibility(ProgressBar.GONE);

    /* -- Button to HOME -- */
    toHome.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent goHome = new Intent(Intent.ACTION_MAIN);
            goHome.setClass(CustomTitleBar.this, MainActivity.class);

            startActivity(goHome);
            finish();
        }
    });

}

}

讓人們告訴我使用完成(); 但它不能解決我的問題。

例如: main > page1 >(單擊主頁)> main > page2 >(單擊主頁)> main

當點擊手機上的返回按鈕時

循環是:main > page2 > main > page1 > main > out of app。

當我使用完成()后單擊移動設備上的后退按鈕時;

循環是:main > main > main > out of app。

在您粘貼的代碼中,您已經明確定義了一個intent go 到MainActivity.class 如果您不希望 go 的主頁按鈕回到您的“mainactivity”,那么您需要定義不同的意圖。 否則,將您不希望主頁按鈕的其他活動中的代碼粘貼到 go 回主。

此外,如果您希望主頁按鈕在您位於主頁時不執行任何操作,那么只需不要設置onClickListener 如果您設置一個偵聽器並將一個intent定義為 go MainActivity ,那么它當然會繼續主...

您可能希望將主要活動的啟動模式定義為singleTop 這樣你就不會得到奇怪的“main -> main -> main”序列。

當您從子頁面導航到主頁時,添加標志 Intent.FLAG_ACTIVITY_CLEAR_TOP。 這是我的示例代碼:

/* -- Button to HOME -- */
toHome.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent goHome = new Intent(Intent.ACTION_MAIN);
        goHome.setClass(CustomTitleBar.this, MainActivity.class);
        goHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(goHome);
        finish();
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM