我有两项活动(Activity1
和Activity2
)和一项AsyncTask(AsyncTask1
)
Activity1
有一个按钮,点按即可控制Activity2
,Activity2
有一个AsyncTask
,而该任务的onPostExecute()
我想要处理{ {1}}中的{1}}。
示例:
活性1
TextView
活性2
Activity1
AsyncTask1
public class Activity1 extends RoboSherlockActivity {
@Inject (R.id.name) EditText name;
@Inject (R.id.mybutton) Button button;
public void onCreate (Bundle savedInstanceState) {
...
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent activity2 = new Intent(context, Activity2.class);
startActivity(activity2);
}
});
....
}
}
问题
如何在AsyncTask的 public class Activity2 extends RoboSherlockActivity {
@InjectView (R.id.name) EditText secret;
....
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Save")
return true;
}
...
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getTitle().toString().equalsIgnoreCase("Save")) {
new AsyncTask1(this, secret.getText().toString()).execute();
}
return true;
}
}
中设置名称?
答案 0 :(得分:0)
Activity2有一个AsyncTask,在那个任务中我想要onPostExecute() 处理Activity1中的TextView。
不,你不能。您可以在Activtiy1中使用startActivtiyForResult
并更新textview
在Activity1中
Intent intent=new Intent(Activity.this,Activity2.class);
startActivityForResult(intent, 2);
然后
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("key");
textView1.setText(message);
}
}
在Activity2中
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
intent = getIntent();
...// rest of the code
}
在onSuccess
protected void onSuccess(String result) {
intent.putExtra("key",value);
setResult(2,intent);
activity.finish();
}
答案 1 :(得分:0)
试试这个......
在Activity1中创建一个sharedPreference,从asynctask store中获取成功结果在sharedPreference&完成活动。
在Activity1的onResume中,从sharedPreference&获取值。如果它不为null,则设置为Textview ..
答案 2 :(得分:0)
我会尝试另一种方法。我不会尝试从AsyncTask更改活动1的元素,而是将AsynTask中的意图发送到活动1。
protected void onSuccess(String result) {
activity.finish();
Intent intent = new Intent("MY_CUSTOM_INTENT");
activity.sendBroadcast(intent);
}
在活动中
@Override
public void onResume(){
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(myHandler, new IntentFilter("MY_CUSTOM_INTENT");
}
BroadcastReceiver myHandler= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//CHANGE HERE THE TEXTVIEW
}
};
答案 3 :(得分:0)
您可以使用静态变量来执行此操作:
public class Activity1 extends RoboSherlockActivity {
...
public static Activity1 instance = null;
public EditText getName(){
return name;
}
public void onCreate (Bundle savedInstanceState) {
...
instance = this;
....
}
public void onDestroy(){
instance = null;
}
}
并调用此方法:
protected void onSuccess(String result) {
activity.finis();
Activity1.instance.getName.setText(result);
//HERE I WANT TO SET THE NAME: name.setText(result); //How can I do this??
}