2014-02-13 32 views
0

如何重載onResume()以正確的方式工作?我想從activity回到MainActivity,我想要在應用程序啓動後擁有相同的狀態。我想要使​​用recreate(),但它打環或某種。重載onResume()

我的代碼:

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    recreate(); 
} 
+0

「我想從活動回到MainActivity,在那裏我想擁有與開始後應用程序相同的狀態」。闡述?你是否產生了另一個你想殺的活動並返回到你的Main?或者你究竟想要什麼? – webuster

+0

@webuster是的,活動可能會被殺死,但最重要的想法是重新加載,刷新所有MainActivity值等。 – user3299430

+0

沒有看到更多的代碼,就很難告訴你爲什麼你正在循環。有一點需要注意的是,應用程序第一次打開時,onResume()會在onCreate()之後調用。意思是你可能會在不期待時調用recreate()。 – natez0r

回答

0

我想,當你按返回鍵,要刷新以前的活動。

這很簡單,你可以把你的方法放在onResume(),它應該做的伎倆。

+0

我不明白應在onResurme()中放置什麼方法? – user3299430

+0

你只想刷新以前的活動?如果是的話,我會舉一些例子? –

+0

是的! :D對我來說非常重要 – user3299430

1

執行onSaveInstanceState(Bundle save)onRestoreInstanceState(Bundle restore)保存和恢復狀態。見the documentation on this.

+0

它會擦除按鈕? – user3299430

+0

很難說。你的問題不是很清楚,但我試圖指出你如何管理android Activity中的狀態。 一般而言,活動的狀態應該保存在'onSaveInstanceState(Bundle save)'中,並在'onRestoreInstanceState(Bundle restore)'中恢復。所以如果你想有一個按鈕有兩個狀態,你可以在'onSaveInstanceState(Bundle save)'和'setButtonVisible(restore.getBoolean(「buttonVisible」,false)''save.putBoolen('buttonVisible',mButtonVisible) '在'onRestoreInstanceState(Bundle restore)'中。 –

0

好吧試試看,它是爲我工作。

public class main extends Activity{ 


    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.your_layout); 

    //add any buttons or anything you have here. 
     doMainWork(); \\lets just say you have this method, which contains the main code of the layout. 
    } 


    protected void onResume(){ 
     super.onResume(); 
     doMainWork(); 
    } 

    public void doMainWork(){ 

     \\Put all your working code here. And this should work it out man. 
    } 
+0

工作,但按鈕仍然可見 – user3299430

+0

您應該編輯了現有的答案。這樣你得到兩個不是很好的答案。我建議你將這個答案的內容移到原來的內容並刪除這個答案。 – Palec

+0

按鈕??好友,你沒有提到任何關於按鈕?它是什麼樣的按鈕? –