When I insert TabActivity inside another TabActivity, startActivityForResult fails when called from inside tabactivity. A new action begins, but an error message appears:
startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent
and onActivityResult is never called when activity returns.
The code to play found below. Four classes, MyActivity is the main class with two tabs, NestedTab has three tabs, all tabs contain SimpleActivity with a button that calls SimpleDialog:
public class MyActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, SimpleActivity.class);
spec = tabHost.newTabSpec("simple").setIndicator("Simple")
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, NestedTab.class);
spec = tabHost.newTabSpec("nested").setIndicator("Nested tabs")
.setContent(intent);
tabHost.addTab(spec);
}
}
public class NestedTab extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
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) {
startActivityForResult(new Intent(mCtx, SimpleDisplay.class), 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
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);
}
}
Unable to insert xml files, but the main one is identical to step 4: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
and simple - it's just a text view and a button.
Any help was appreciated.