Android: Nested tabactivities doesn't work with startActivityForResult
When I nest a TabActivity inside another TabActivity, startActivityForResult fails when called from the inner tabactivity. The new activity starts, but I get an error message:
startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent
and onActivityResult is never called when the activity returns.
Code to reproduce found below. Four classes, MyActivity is the main class with two tabs, NestedTab has three tabs, all tabs containt SimpleActivity with a button that calls SimpleDialog:
public class MyActivity extends TabActivity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Populate a couple of tabs
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Crea开发者_如何学编程te an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, SimpleActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("simple").setIndicator("Simple")
.setContent(intent);
tabHost.addTab(spec);
// Repeat
intent = new Intent().setClass(this, NestedTab.class);
spec = tabHost.newTabSpec("nested").setIndicator("Nested tabs")
.setContent(intent);
tabHost.addTab(spec);
}
}
public class NestedTab extends TabActivity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent(this, SimpleActivity.class);
String tabs[]= {"One", "Two", "Three"};
for (String s : tabs)
{
intent.putExtra("name", s);
spec = tabHost.newTabSpec(s).setIndicator(s).setContent(intent);
tabHost.addTab(spec);
}
}
public class SimpleActivity extends Activity {
Button mBtn;
Context mCtx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple);
mCtx = this;
mBtn = (Button) findViewById(R.id.btn);
mBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//To change body of implemented methods use File | Settings | File Templates.
startActivityForResult(new Intent(mCtx, SimpleDisplay.class), 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); //To change body of overridden methods use File | Settings | File Templates.
Toast.makeText(this, "Activity finished", Toast.LENGTH_LONG).show();
}
}
public class SimpleDisplay extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
text.setText("Just hit back button");
setContentView(text);
}
}
Not able to paste the xml files but main is identical to step 4: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
and simple is just a textview and a button.
Any help appreciated.
Try SimpleActivity.this.startActivity
or ((Activity) view.getContext()).startActivity
instead of just startActivity
. You're firing off the Activity from your OnClickListener
instead of from your Activity
otherwise, hence the error and the lack of callback to the Activity's onActivityResult
.
EDIT: Ah, I missed the double-nested bit. Things get all kind of weird when you double-nest TabActivity with intents at both levels -- look at the source code for TabActivity
to see what goes on there, and you should be able to emulate its behavior if you really want to.
The long and short of it is that your onActivityResult will only get propagated down one level; if you really really want to create an Intent-based sub-subtab (as opposed to Intent-based tabs with normal view-based subtabs) you'll need to have the parent (single-nested) tab fire off the Intent. To do that, try setting your single-nested tab to have a method like called something like startActivityAndDispatchToChild
, and call that from the child with getParent().startActivityAndDispatchToChild
. Have that method startActivityForResult, and override the getActivityResult
method on the single-nested tab use the code coming back from the child to figure out which child to dispatch to. Using that code, call getLocalActivity().getActivity(whateverTag)
to dispatch to the proper subactivity.
But don't do that. Just have your second-level activity use View-based rather than Intent-based content; if you really want to separate out what the sub-sub-pages do somewhat, write a custom ViewGroup class to manage the content for each subtab.
精彩评论