Java 在 Android 中的选项卡内启动活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1306689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launching activities within a tab in Android
提问by MattC
Here's the deal. I have an application with three tabs. Through various interactions with the items in the tabs I end up launching other activities. The client has reviewed this and would like the activities launched "within" the tabs, so the tabs remain visible and if the user clicks the tab it goes back to the original activity defined in the setContent function. Is this possible and how would I go about this from other activities? (ie the child activities, not the one that defines the TabHost and has access to call setContent)?
这是交易。我有一个包含三个选项卡的应用程序。通过与选项卡中项目的各种交互,我最终启动了其他活动。客户已经了这一点,并希望在选项卡“内”启动活动,因此选项卡保持可见,如果用户单击选项卡,它会返回到 setContent 函数中定义的原始活动。这可能吗,我将如何从其他活动中解决这个问题?(即子活动,而不是定义 TabHost 并有权调用 setContent 的活动)?
采纳答案by hcpl
It is possible to launch activities within tabs. Therefore set the tabspec content to an ActivityGroup instead of a regular Activity.
可以在选项卡中启动活动。因此,将 tabspec 内容设置为 ActivityGroup 而不是常规活动。
tabHost.addTab(tabHost.newTabSpec("Tab")
.setIndicator("Tab")
.setContent(new Intent(this, YourActivityGROUP.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
From within that ActivityGroup you can then start another Activity like this that only updates the contentview of the tab you're in.
从该 ActivityGroup 中,您可以启动另一个这样的 Activity,该 Activity 仅更新您所在选项卡的内容视图。
class YourActivityGROUP extends ActivityGroup{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//you van get the local activitymanager to start the new activity
View view = getLocalActivityManager()
.startActivity("ReferenceName", new
Intent(this,YourActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
this.setContentView(view);
}
}
回答by marcc
commonsware.com is correct, it's not possible. I had a similar issue, but it was only 1 activity which was being launched. I sacrificed a little of my architecture and deleted the activity which was launched from inside the tab. I put the code in a View and then I added a ViewAnimator to the tab's activity. I overrode the back button and remove that view if it's up, or else let the back button perform as normal.
commonsware.com 是正确的,这是不可能的。我有一个类似的问题,但只有 1 个活动正在启动。我牺牲了一些架构并删除了从选项卡内部启动的活动。我将代码放在一个视图中,然后在选项卡的活动中添加了一个 ViewAnimator。我覆盖了后退按钮并删除该视图(如果它已启动),或者让后退按钮正常执行。
This faked it well enough, and for only 1 closely-related activity, I'm not going to lose any sleep over the design considerations.
这很好地伪造了它,对于只有 1 个密切相关的活动,我不会因为设计考虑而失眠。
回答by Premier
I solved with this: Experience - Multiple Android Activities in a TabActivity
我解决了这个问题:体验 - TabActivity 中的多个 Android 活动
回答by Georgy Gobozov
Here is my solution
这是我的解决方案
public class ActivityStack extends ActivityGroup {
private Stack<String> stack;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (stack == null) stack = new Stack<String>();
//start default activity
push("FirstStackActivity", new Intent(this, FirstStackActivity.class));
}
@Override
public void finishFromChild(Activity child) {
pop();
}
@Override
public void onBackPressed() {
pop();
}
public void push(String id, Intent intent) {
Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
stack.push(id);
setContentView(window.getDecorView());
}
}
public void pop() {
if (stack.size() == 1) finish();
LocalActivityManager manager = getLocalActivityManager();
manager.destroyActivity(stack.pop(), true);
if (stack.size() > 0) {
Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
Window newWindow = manager.startActivity(stack.peek(), lastIntent);
setContentView(newWindow.getDecorView());
}
}
}
Launch tab
启动选项卡
Intent intent = new Intent().setClass(this, ActivityStack.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tabId")
spec.setContent(intent);
Call next activity
调用下一个活动
public class FirstStackActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("First Stack Activity ");
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getParent(), SecondStackActivity .class);
ActivityStack activityStack = (ActivityStack) getParent();
activityStack.push("SecondStackActivity", intent);
}
});
setContentView(textView);
}
}
Call next again
下次再打电话
public class SecondStackActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("First Stack Activity ");
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getParent(), ThirdStackActivity .class);
ActivityStack activityStack = (ActivityStack) getParent();
activityStack.push("ThirdStackActivity", intent);
}
});
setContentView(textView);
}
}
Works on emulator 2.2
适用于模拟器 2.2
回答by Danny
How about 2 tabbar in this problem. First 1 is menu bottom tabbar, second one is top tabbar, they are different activity and xml
这个问题中的 2 tabbar 怎么样。第一个是菜单底部标签栏,第二个是顶部标签栏,它们是不同的活动和 xml
回答by Mohammed Elsabry
you can use
您可以使用
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
for each activity you set as a content for tabSpec, and it will create this activity each time you press on the tab
对于您设置为 tabSpec 内容的每个活动,每次按下选项卡时它都会创建此活动
回答by Rafel C.F
As I can open a fragment or activity from a spinner in the toolbar ?
因为我可以从工具栏中的微调器打开片段或活动?
import android.content.Intent; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log; import android.view.Menu;
import android.view.MenuItem; import android.view.View;
import android.widget.AdapterView; import android.widget.ArrayAdapter;
import android.widget.Spinner;
> public class MainActivity extends AppCompatActivity {
>
> @Override
> protected void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.activity_main);
>
> //Appbar
> Toolbar toolbar = (Toolbar) findViewById(R.id.appbar);
> setSupportActionBar(toolbar);
> getSupportActionBar().setDisplayShowTitleEnabled(false);
>
> //Appbar page filter
> Spinner cmbToolbar = (Spinner) findViewById(R.id.CmbToolbar);
>
> ArrayAdapter<String> adapter = new ArrayAdapter<>(
> getSupportActionBar().getThemedContext(),
> R.layout.appbar_filter_title,
> new String[]{"Opción 1 ", "Opción 2 ", "Opción 3 "});
>
> adapter.setDropDownViewResource(R.layout.appbar_filter_list);
>
> cmbToolbar.setAdapter(adapter);
>
> cmbToolbar.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
> @Override
> public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
> //... Acciones al seleccionar una opción de la lista
> Log.i("Toolbar 3", "Seleccionada opción " + position);
>
> Fragment f = null;
>
> switch(position) {
> case 0:
> f = Fragment2.newInstance();
> break;
>
> case 1:
> f = Fragment1.newInstance();
> break;
>
> }
> }
>
> @Override
> public void onNothingSelected(AdapterView<?> adapterView) {
> //... Acciones al no existir ningún elemento seleccionado
> }
> });
> }
>
> @Override
> public boolean onCreateOptionsMenu(Menu menu) {
> // Inflate the menu; this adds items to the action bar if it is present.
> getMenuInflater().inflate(R.menu.menu_main, menu);
> return true;
> }
>
> @Override
> public boolean onOptionsItemSelected(MenuItem item) {
> // Handle action bar item clicks here. The action bar will
> // automatically handle clicks on the Home/Up button, so long
> // as you specify a parent activity in AndroidManifest.xml.
> int position = item.getItemId();
>
> Fragment f = null;
>
> switch(position) {
> case 0:
> f = Fragment2.newInstance();
> break;
>
> case 1:
> f = Fragment1.newInstance();
> break;
>
> case 2:
> Intent intent = new Intent(getApplicationContext(), Fragment1.class);
> startActivity(intent);
> break;
>
> }
> return super.onOptionsItemSelected(item);
> }
>
>
> public Fragment getItem(int position) {
>
> Fragment f = null;
>
> switch(position) {
> case 0:
> f = Fragment2.newInstance();
> break;
>
> case 1:
> f = Fragment1.newInstance();
> break;
>
> case 2:
> Intent intent = new Intent(getApplicationContext(), Fragment1.class);
> startActivity(intent);
> break;
>
> }
>
> return f;
> } }