6.26在TabView中导航不同的活动
问题
您要从标签视图中的活动更改为同一标签中的另一个活动。
解
使用要移动到的新活动替换标签页的内容视图。
讨论
当TabView中的“调用”活动通过意图调用另一个活动时,TabView将被被调用活动的视图替换。 要在TabView中显示被调用的活动,我们可以用调用活动的视图替换调用活动的视图,以便TabView保持稳定。 要实现这一点,我们需要从ActivityGroup而不是Activity扩展调用活动。
在示例6-32中,从TabView中设置了从ActivityGroup扩展的调用活动。
问题
您要从标签视图中的活动更改为同一标签中的另一个活动。
解
使用要移动到的新活动替换标签页的内容视图。
讨论
当TabView中的“调用”活动通过意图调用另一个活动时,TabView将被被调用活动的视图替换。 要在TabView中显示被调用的活动,我们可以用调用活动的视图替换调用活动的视图,以便TabView保持稳定。 要实现这一点,我们需要从ActivityGroup而不是Activity扩展调用活动。
在示例6-32中,从TabView中设置了从ActivityGroup扩展的调用活动。
实例6-32。 替换选项卡中的活动
//'Calling' activity.
public class Calling extends ActivityGroup implements OnClickListener
{
Button b1;
Intent i1;
/** Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calling);
b1=(Button)findViewById(R.id.changeactivity);
b1.setOnClickListener();
}
public void onClick(View view) {
// This creates an intent to call the 'Called' activity
i1=new Intent(this.getBaseContext(),Called.class);
// calls the method to replace View.
replaceContentView("Called", i1);
}
// This method is used to replace the view of 'Calling' activity by 'Called' activity.
public void replaceContentView(String id, Intent newIntent) {
// Obtain the view of 'Called' activity using its Intent 'newIntent'
View view = getLocalActivityManager().startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
//set the above view to the content of 'Calling' activity.
this.setContentView(view);
}
“被叫活动”也可以调用另一个活动(称为CalledSecond),如下所示:
}//'Called activity'
public class Called extends Activity implements OnClickListener
{
Button b1;
Intent i1;
Calling caller;
/** Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.called);
b1=(Button)findViewById(R.id.changeactivity);
b1.setOnClickListener();
}
public void onClick(View view) {
// This creates an intent to call the 'CalledSecond' activity
i1=new Intent(this.getBaseContext(),CalledSecond.class);
/* 'CalledSecond' can be any activity, even the
* 'Calling'(In case backward navigation is required)
*/
// Initialize the object of the 'Calling' class.
caller=(Calling)getParent();
// calls the method to replace View.
caller.replaceContentView("CalledSecond", i1);
}
}