2011-06-05 85 views
0

我在自定義標題欄上創建主頁按鈕(使用圖片到按鈕)。 我的問題是每次點擊這個按鈕。它將會主要。 當停留在主頁面並點擊按鈕。它會一次又一次地到主頁。 我該怎麼辦? 在主頁面時我不想去主要或者不能在主頁面點擊這個按鈕。自定義標題欄上的Android主頁按鈕

你明白嗎?

請幫我 謝謝

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

} 

} 

纔有人告訴我用面漆();但它無法解決我的問題。

從例如:主>第1頁>(點擊家)>主>第2頁>(點擊家)>主

當移動

週期點擊返回按鈕是:主>第2頁>主>第1頁>主要>出於應用程序。

當我使用finish()後,在手機上點擊返回按鈕;

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

回答

0

當您從子頁面導航到主頁時,添加標誌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(); 
    } 
}); 
+0

非常感謝,但如何使用它? – 2011-06-05 15:59:53

+0

哈哈,這是:toHome.setOnClickListener(新OnClickListener(){ 公共無效的onClick(視圖v){ 意向意圖=新意圖(這一點,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity (intent); finish(); } }); – anticafe 2011-06-05 16:29:03

+0

構造函數Intent(new View.OnClickListener(){},Class )未定義.... – 2011-06-05 16:41:12

1

在您粘貼的代碼中,您明確定義了一個intent以轉至MainActivity.class。如果你不想讓主頁按鈕回到你的「主要活動」,那麼你需要定義一個不同的意圖。否則,將您不希望home按鈕回到main的其他活動中的代碼粘貼。

此外,如果您希望主頁按鈕在您進入主頁面時不做任何操作,則不要設置onClickListener。如果設置一個監聽器和定義一個intentMainActivity,那當然會繼續將主...

+0

非常感謝。 – 2011-06-05 16:03:20

0

你可能要定義你的主要活動的啓動模式爲singleTop。這樣你就不會得到奇怪的「main - > main - > main」序列。

+0

非常感謝。我測試使用的結果是,當停留在主要點擊按鈕都將關閉。 – 2011-06-05 16:16:36