2012-01-19 141 views
3

這是我的第一篇文章,所以請好:) 我有一個問題,沒有人給我在看過後回答。刷新活動並重新打開

我的應用程序列表和一個按鈕,打開一個活動,該活動將創建一個新的項目按下按鈕,創建時,在前面的活動列表中顯示。

我該怎麼做?

這是我的第一個按鈕使得代碼:

intent = new Intent(this.getBaseContext(), NewTestActivity.class); 
this.finish(); 
startActivity(intent); 

,這是代碼回去的刷新:

Intent intent = new Intent(getBaseContext(), TestListActivity.class);      
startActivity(intent); 

但代碼GoBack的是有用的,因爲活動不刷新。 我必須以不同的方式調用新的活動?或者以不同的方式回到previus活動?或者當我回到previus活動時正常恢復並刷新活動?

呃......這就是全部。 對不起,我的英語不好,如果這個問題已經在另一個線程中回答,請給我鏈接閱讀,因爲我找不到它:)

PS:我從12月開始用android。

感謝您的幫助。

+0

你在onResume()方法中有代碼嗎?通常onResume將被調用來進行後續的活動調用。 – kosa

+0

不,加載列表的動作是在onCreate()方法中。 – AuTi

+0

嘗試在onResume()中添加同樣的東西並查看。 – kosa

回答

1
  1. 當你要開始新的活動時,你不應該關閉當前的活動直到你真的需要這種行爲。 (從代碼中刪除this.finish();行)
  2. 你也不能手動關閉活動的活動,直到你真正需要它。當用戶按下設備上的「後退」按鈕時,Android將從「後退堆棧」彈出前一個活動。再次閱讀Activity文檔。
  3. 根據你的描述你有一個元素列表。因此,爲了刷新列表,您需要更新數據集並通過ListView.notifyDataSetChanged()方法調用通知列表。
1

之前得到一個真正的答案,我想展現給你的代碼的一些改進。 首先,創建一個Intent(或者當你需要在一般情況下),從活動中沒有必要調用getBaseContext()時,你可以使用this

intent = new Intent(this, NewTestActivity.class); 

其次,Android是善於處理活動,您不必通過finish()手動關閉第一個活動。 Android會自動暫停或停止您的第一個活動,並在您返回時將其恢復。

第三,在你的情況下,你可能想使用startActivityForResult()而不是startActivity(),我將在下面解釋。
這將會使你的代碼如下所示:

private static final int MY_REQUEST_CODE = 33487689; //put this at the top of your Activity-class, with any unique value. 
intent = new Intent(this, NewTestActivity.class); 
startActivityForResult(intent, MY_REQUEST_CODE); 

現在,startActivityForResult()開始的活動,並等待來自新的活動的結果。當您在新的活動叫finsih()你最終會在第一Activitys onActivityResult() - 方法,與新Activty提供的數據現在已經關閉:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode != MY_REQUEST_CODE) return; //We got a result from another request, so for this example we can return. 
    if(resultCode != RESULT_OK) return; //The user aborted the action, so we won't get any data. 
    //After the above if-statements we know that the activity that gives us a result was requested with the correct request code, and it's action was successful, we can begin extracting the data, which is in the data-intent: 
    Item item = (Item) data.getSerializableExtra("customData"); //casts the data object to the custom Item-class. This can be any class, as long as it is serializable. There are many other kinds of data that can be put into an intent, but for this example a serializable was used. 
    itemList.add(item); //This is the list that was specified in onCreate() 
    //If you use an Adapter, this is the place to call notifyDataSetChanged(); 
} 

對於這一切工作,我們需要做一些事情在第二個活動: 當該項目已被創建,我們必須設置一個結果:

//We begin by packing our item in an Intent (the Item class is an example that is expected to implement Serializable) 
Item theCreatedItem; //This is what was created in the activity 
Intent data = new Intent(); 
data.putSerializable(theCreatedItem); 
setResult(RESULT_OK, data); 
finish(); 

這應該返回到第一Activitys onActivityResult() - 方法與項目,如上所述。

+0

waw ...真的很明確......但是,我認爲有一個最好的方式來做我想做的事情。 只需要刷新活動,每次我顯示...沒有什麼奇怪的。 真的非常感謝Jave ...你是一位了不起的老師! – AuTi

+0

很好,祝你的項目好運:) – Jave

+0

好吧......就像我說的,解決方案非常簡單,我只想在另一個類的功能上將運營商私有化改爲公開。然後使用更新列表功能,無論我想:) 非常感謝您的幫助! – AuTi