简体   繁体   中英

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. 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.

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

when click back button on mobile

cycle is: 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.

In the code you've pasted, you have explicitly defined an an intent to go to the MainActivity.class . If you don't want the home button to go back to your "mainactivity" then you need to define a different intent. Otherwise, paste the code from your other activities where you don't want the home button to go back to main.

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 . If you set a listener and define an intent to go MainActivity , then of course it'll keep going to main...

You probably want to define your main activity's launch mode to be singleTop . That way you won't get the weird "main -> main -> main" sequence.

Add flag Intent.FLAG_ACTIVITY_CLEAR_TOP when you navigate from sub page to home. 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();
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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