gpt4 book ai didi

android - 在子 Activity 关闭时更新 Activity

转载 作者:搜寻专家 更新时间:2023-11-01 09:02:36 25 4
gpt4 key购买 nike

我有一个像这样的 Activity 堆栈:

activity A make something

activity A call Activity B (without finish();)

activity B make something

activity B call activity C

activity B finish();

activity C make something

activity C finish();

我想更新 Activity A 的 View ,因为 Activity C 所做的修改更新了数据库,也许我有不同的东西要在 Activity A 中列出。

如果我的主要 Activity 是这样的:

public class MainActivity extends ListActivity {
private pollDataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

datasource = new pollDataSource(this);
datasource.open();

Cursor values = datasource.getAllCategorie();

String[] categorieColumns =
{
MySQLiteHelper.COLUMN_NOME // Contract class constant containing the word column name

};


int[] mWordListItems = { R.id.categoria_label };


SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), // The application's Context object
R.layout.single_list_item, // A layout in XML for one row in the ListView
values, // The result from the query
categorieColumns, // A string array of column names in the cursor
mWordListItems, // An integer array of view IDs in the row layout
0); // Flags (usually none are needed)


setListAdapter(adapter);
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

TextView textView = (TextView) v.findViewById(R.id.categoria_label);
String text = textView.getText().toString();

Intent myIntent = new Intent(MainActivity.this, domandeDiCategoria.class);
myIntent.putExtra("categoriaName", text);
myIntent.putExtra("categoriaId", id);
datasource.close();
MainActivity.this.startActivity(myIntent);

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}




}

我必须编写 onResume() 吗?并使其类似于onCreate?或者我可以使用相同的游标、相同的适配器等来“更新” View ?什么更好?

最佳答案

1)在A Activity 中定义

public static int UPDATE_A_ACTIVITY  = 1;

2) startActivityForResult(intentB, A.UPDATE_A_ACTIVITY);//从 A 开始 B

3) startActivityForResult(intentC, A.UPDATE_A_ACTIVITY);//从 B 开始 C

4) 当 C activity 一切正常时,完成 C activity 并通知 A activity,他应该更新一些东西

在 C Activity 中:

 finish();
setResult(RESULT_OK);

5) 在调用 setResult(RESULT_OK); 后将调用 A 中的下一个方法(在 A Activity 中写入):

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case UPDATE_A_ACTIVITY:
if (resultCode == RESULT_OK) {
// update what u want
}
break;
}
}

关于android - 在子 Activity 关闭时更新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13991275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com