开发者

Android Home button on custom title bar

开发者 https://www.devze.com 2023-03-11 15:45 出处:网络
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.

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 : ma开发者_C百科in > 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...


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消