我希望能够在用户单击按钮时重新启动 Activity 。最重要的是,在我重新启动后,我希望选项卡主机和选项卡仍然显示。
问题:
目前,当我重新启动 Activity 时,选项卡主机和选项卡在重新创建后消失。
我对这个问题进行了大量研究,但是我的代码的复杂性具有挑战性,其他答案对我的情况没有帮助。
到目前为止采用的代码和当前方法:
我使用 tabHost 创建了四个选项卡:
INBOX_SPEC - InboxActivity 类
OUTBOX_SPEC - OutActivity 类
PROFILE_SPEC - ProfileActivity 类
LPGA_SPEC - LPGA Activity 类
在 InboxActivity 类中,当用户单击按钮时我想重新启动 InboxActivity。
代码:InboxActivity类:
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
Intent intent = getIntent();
//destroyActivity(intent);
startActivity(intent);
// Intent refresh = new Intent();
// startActivity(refresh);//Start the same Activity
// finish(); //finish Activity.
// InboxActivity.refreshContent();
}
});
代码:在 TabAndListView 类中创建 Tabhost:
TabHost tabHost = getTabHost();
getActionBar().hide();
// Inbox Tab
TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
// Tab Icon
inboxSpec.setIndicator(INBOX_SPEC, getResources().getDrawable(R.drawable.icon_inbox));
Intent inboxIntent = new Intent(this, InboxActivity.class);
// Tab Content
inboxSpec.setContent(inboxIntent);
// Outbox Tab
TabSpec outboxSpec = tabHost.newTabSpec(OUTBOX_SPEC);
outboxSpec.setIndicator(OUTBOX_SPEC, getResources().getDrawable(R.drawable.icon_outbox));
Intent outboxIntent = new Intent(this, OutboxActivity.class);
outboxSpec.setContent(outboxIntent);
// Profile Tab
TabSpec profileSpec = tabHost.newTabSpec(PROFILE_SPEC);
profileSpec.setIndicator(PROFILE_SPEC, getResources().getDrawable(R.drawable.icon_profile));
Intent profileIntent = new Intent(this, ProfileActivity.class);
profileSpec.setContent(profileIntent);
// LPGA_SPEC Tab
TabSpec LPGASpec = tabHost.newTabSpec(LPGA_SPEC);
LPGASpec.setIndicator(LPGA_SPEC, getResources().getDrawable(R.drawable.icon_profile));
Intent LPGAIntent = new Intent(this, WomenActivity.class);
LPGASpec.setContent(LPGAIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(inboxSpec); // Adding Inbox tab
tabHost.addTab(outboxSpec); // Adding Outbox tab
tabHost.addTab(profileSpec); // Adding Profile tab
tabHost.addTab(LPGASpec); // Adding Profile tab
}
最佳答案
您只需重新运行您在以下位置填充 ListView 的代码:
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//Rerun the code where you populate the listView here
}
});
然后确保 ListView 在重新填充之前已被重置
关于java - 我如何在 TabHosts 中重新填充我的 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665770/