从 ListActivity 调用特定方法,该方法调用父(TabActivity)之一中的方法,而不会丢失 TabHost 布局
我在 TabHost 1.PlayingActivity 2.AlbumActivity 中有两个活动
通过单击 AlbumActivty 中的按钮将跳转到 LIstActivity -> 通过单击 ListActivity 中的项目,我想跳回到其中的方法之一 - PlayingActivity w/o Losing Tab Layout。
我可以通过使用这些调用 Activity n 特定方法来完成任务
ListActivity 点击 Item 调用 PlayingActivity 中的特定方法
Class SongList extends ListActivty
{
public void onCreate(Bundle savedInstanceState)
{
//implemented list adapter-ls
listadapter. . . .
ls.setOnItemClickListener(New View.onItemClickListener)
{
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
int songIndex=position;
Intent i=new Intent(getApplicationContext(),AlbumActivity.class);
i.putExtra("methodName", "myMethod");
i.putExtra("index", songIndex);
startActivity(i);
}
}
}
}
在 TabHost 下托管 PlayingActivity 和 AblumActivity 的 MainActivity
public class MainActivity extends TabActivity {
private static final String NOW_PLAYING = "Playing";
private static final String ALBUM = "Album";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Playing Tab
TabSpec PlayingSpec = tabHost.newTabSpec(NOW_PLAYING);
// Tab Icon
PlayingSpec.setIndicator(NOW_PLAYING, getResources().getDrawable(R.drawable.icon_now_playing));
Intent PlayingIntent = new Intent(this, PlayingActivity.class);
// Tab Content
PlayingSpec.setContent(PlayingIntent);
// Album Tab
TabSpec AlbumSpec = tabHost.newTabSpec(ALBUM);
AlbumSpec.setIndicator(ALBUM, getResources().getDrawable(R.drawable.icon_music));
Intent AlbumIntent = new Intent(this, AlbumActivity.class);
AlbumSpec.setContent(AlbumIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(PlayingSpec); // Adding Playing tab
tabHost.addTab(AlbumSpec); // Adding Album tab
}
}
在 PlayingActivity 中调用特定方法(playSong())
class PlayingActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String index=intent.getStringExtra("index");
if(intent.getStringExtra("methodName").equals("myMethod")){
playSong(Integer.parseInt(index));
}
}
private void playSong(int i)
{
}
}