当我将TabActivity嵌套在另一个TabActivity中时,startActivityForResult在从内部tabactivity调用时失败。新活动开始,但我收到一条错误消息:
startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent
并且在活动返回时永远不会调用onActivityResult。
重现的代码如下所示。四个类,MyActivity是带有两个选项卡的主类,NestedTab有三个选项卡,所有选项卡都包含SimpleActivity和一个调用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
// Create 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);
}
}
无法粘贴xml文件,但main与步骤4相同:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
简单只是一个文本视图和一个按钮。
任何帮助表示感谢。
答案 0 :(得分:3)
尝试SimpleActivity.this.startActivity
或((Activity) view.getContext()).startActivity
而非startActivity
。您正在从OnClickListener
而不是Activity
开始执行活动,因此错误以及缺少对活动onActivityResult
的回调。
TabActivity
的源代码,看看那里发生了什么,如果你真的想要你应该能够模仿它的行为到。
它的长短是你的onActivityResult只会传播到一个层次;如果你真的真的想要创建一个基于Intent的子子选项卡(而不是基于Intent的选项卡和普通的基于视图的子选项卡),你需要让父(单一嵌套)选项卡触发Intent。要做到这一点,请尝试将单一嵌套选项卡设置为类似startActivityAndDispatchToChild
之类的方法,并使用getParent().startActivityAndDispatchToChild
从子项中调用该方法。使用该方法startActivityForResult,并覆盖单嵌套选项卡上的getActivityResult
方法,使用从子进程返回的代码来确定要分派给哪个子进程。使用该代码,调用getLocalActivity().getActivity(whateverTag)
以调度到适当的子活动。
但不要这样做。让你的第二级活动使用基于视图而不是基于意图的内容;如果你真的想要分离出子子页面的作用,可以编写一个自定义的ViewGroup类来管理每个子表的内容。