简体   繁体   English

Android 自定义标题栏上的主页按钮

[英]Android Home button on custom title bar

I built home button on custom titlebar (use picture to button).我在自定义标题栏上构建了主页按钮(使用图片到按钮)。 My Problem is every time to click this button.我的问题是每次单击此按钮。 It will go to main.它将 go 为主。 When stay in main page and click the button.当停留在主页并单击按钮时。 It will to main page again and again.它将一次又一次地进入主页。 How I do??我怎样做?? I want it not go to main when stay main or Can't click this button in main page.我希望它不是 go 在保持主要或无法在主页中单击此按钮时变为主要。

Are you understand?明白了吗?

Please help me Thank you请帮帮我谢谢

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();
        }
    });

}

}

have people tell me to use finish();让人们告诉我使用完成(); but it can't fix my problem.但它不能解决我的问题。

from example: main > page1 > (click home) > main > page2 > (click home) > main例如: main > page1 >(单击主页)> main > page2 >(单击主页)> main

when click back button on mobile当点击手机上的返回按钮时

cycle is: main > page2 > main > page1 > main > out of app.循环是:main > page2 > main > page1 > main > out of app。

when click back button on mobile after I use finish();当我使用完成()后单击移动设备上的后退按钮时;

cycle is: main > main > main > out of app.循环是:main > main > main > out of app。

In the code you've pasted, you have explicitly defined an an intent to go to the MainActivity.class .在您粘贴的代码中,您已经明确定义了一个intent go 到MainActivity.class If you don't want the home button to go back to your "mainactivity" then you need to define a different intent.如果您不希望 go 的主页按钮回到您的“mainactivity”,那么您需要定义不同的意图。 Otherwise, paste the code from your other activities where you don't want the home button to go back to main.否则,将您不希望主页按钮的其他活动中的代码粘贴到 go 回主。

Also, if you want the home button to not do anything when you're in the main page, then simply don't set an onClickListener .此外,如果您希望主页按钮在您位于主页时不执行任何操作,那么只需不要设置onClickListener If you set a listener and define an intent to go MainActivity , then of course it'll keep going to main...如果您设置一个侦听器并将一个intent定义为 go MainActivity ,那么它当然会继续主...

You probably want to define your main activity's launch mode to be singleTop .您可能希望将主要活动的启动模式定义为singleTop That way you won't get the weird "main -> main -> main" sequence.这样你就不会得到奇怪的“main -> main -> main”序列。

Add flag Intent.FLAG_ACTIVITY_CLEAR_TOP when you navigate from sub page to home.当您从子页面导航到主页时,添加标志 Intent.FLAG_ACTIVITY_CLEAR_TOP。 Here's my example code:这是我的示例代码:

/* -- 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