Android wait for another activity result - it didn't work
How to wait to another activity result in android. I have written the following code but after the activity finishes, it always goes to onCreate() method.
I have tab activity.From one tab activity , i called another activity, that is popup window......After finished popup window It should go back to previous activity
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
keyword = o.toString();
positions = position;
if(position != 0 ){
Bundle bundle = new Bundle();
Intent showContent = new Intent(getApplicationContext(),SalesRouteDevitionActivity.class);
int postion = position;
String aString = Integer.toString(postion);
bundle.putString("positon", aString);
showContent.putExtras(bundle);
startActivityForResult(showContent,0);
}else{
Intent intent = new Intent(SalesRouteActivity.this, ListRetailerActivity.class);
Bundle bundle = new Bundle();
bundle.putString("RouteName", keyword);
intent.putExtras(bundle);
View view = SalesActivityGroup.group.getLocalActivityManager().startActivity("", intent.开发者_如何学CaddFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
SalesActivityGroup.group.replaceView(view);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.w("onActivityResult" ," --requestCode ---" + requestCode + " -- resultCode -- " + resultCode + " -- data --" + data);
if(resultCode==0){
Toast.makeText(this, "Reason has been successfully.", Toast.LENGTH_LONG).show();
if(resultCode==RESULT_OK)
Toast.makeText(this, "Reason has been successfully.", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
}
}
and SalesRouteDevitionActivity.class
public class SalesRouteDevitionActivity extends Activity {
private String array_spinner[];
String param1 = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.route_diviation_popup);
array_spinner=new String[2];
array_spinner[0]="Rain";
array_spinner[1]="Floods";
Spinner s = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, array_spinner);
s.setAdapter(adapter);
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent mIntent = new Intent();
setResult(RESULT_OK);
finish();
}
});
}
}
This is my Tab Activity.I am using ActivityGroup
public class MainActivity extends TabActivity {
int selectedTab;
TabHost tabHost ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);
TabHost t = getTabHost();
tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
/** TabSpec setIndicator() is used to set name for the tab. */
/** TabSpec setContent() is used to set content for a particular tab. */
firstTabSpec.setIndicator("Sales").setContent(new Intent(this,SalesActivityGroup.class));
secondTabSpec.setIndicator("Admin").setContent(new Intent(this,SettingActivityGroup.class));
thirdTabSpec.setIndicator("Setting").setContent(new Intent(this,SettingActivityGroup.class));
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
tabHost.setCurrentTab(0);
tabHost.setMinimumHeight(25);
}
public void onTabChanged(String arg0) {
selectedTab = tabHost.getCurrentTab();
}
This is SalesActivityGroup.java
public class SalesActivityGroup extends ActivityGroup {
public static SalesActivityGroup group;
private ArrayList<View> history;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
View view = getLocalActivityManager().startActivity("Sales",
new Intent(this, SalesRouteActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
replaceView(view);
}
public void replaceView(View v) {
history.add(v);
setContentView(v);
}
public void back() {
if (history.size() > 0) {
history.remove(history.size() - 1);
if (history.size() > 0) {
setContentView(history.get(history.size() - 1));
} else {
finish();
}
} else {
finish();
}
}
@Override
public void onBackPressed() {
SalesActivityGroup.group.back();
return;
}
}
I know this is the way we should do, but really i dont know why its not working my part....
I didn't go to onActivityResult() method & it didnt print Log
also
Please help me..
Thanks in advance
Remove startActivity(showContent);
from SalesRouteDevitionActivity.class
Edit:
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
You do not need to call
super.onActivityResult(requestCode, resultCode, data);
when you are handling ActivityResult.
Replace your System.out.println with Log.i
replace this
if(resultCode==0)
with this
if(requestCode==0)
There is no console in Android. So System.out.println
won't work. For debug purposes, you can use the LogCat
. Here's how to use LogCat
If you are using tab ActivityGroup then should use like this:
Calling activity:
Intent showContent = new Intent(botton.getContext(),RetailerOrderEActivity.class);
getParent().startActivityForResult(showContent, 2);
consider getParent()
, If it is Tab activityGroup then call like that.
and child Activity:
Intent i = new Intent();
Bundle bundle = new Bundle();
i.putExtras(bundle);
setResult(Activity.RESULT_OK, i);
finish();
after finish this will go to ActivityGroup class:
there you want to call like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode ==1){
Bundle bundle = data.getExtras();
String roteCode = bundle.getString("RouteCode");
Intent intent = new Intent(SalesActivityGroup.this,ListRetailerActivity.class);
bundle.putString("RouteCode", roteCode);
intent.putExtras(bundle);
View view = SalesActivityGroup.group.getLocalActivityManager().startActivity("",intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
SalesActivityGroup.group.replaceView(view);
}
}
精彩评论