在Android中的选项卡中启动活动

时间:2009-08-20 14:46:43

标签: java android

这是交易。我有一个带有三个标签的应用程序。通过与选项卡中的项目的各种交互,我最终启动了其他活动。客户端已对此进行了审核,并希望在选项卡内“启动”活动,因此选项卡仍然可见,如果用户单击选项卡,则会返回到setContent函数中定义的原始活动。这是可能的,我将如何从其他活动中解决这个问题? (即子活动,而不是定义TabHost并有权调用setContent的那个)?

7 个答案:

答案 0 :(得分:44)

可以在标签中启动活动。因此,将tabspec内容设置为ActivityGroup而不是常规Activity。

tabHost.addTab(tabHost.newTabSpec("Tab")
                .setIndicator("Tab")
                .setContent(new Intent(this, YourActivityGROUP.class)
                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

然后,您可以从该ActivityGroup中启动另一个此类活动,该活动仅更新您所在选项卡的内容视图。

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);

   }
}

答案 1 :(得分:11)

这是我的解决方案

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());
    }
  }
}

启动标签

Intent intent = new Intent().setClass(this, ActivityStack.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tabId")
spec.setContent(intent);

调用下一个活动

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);
  }
}

再次拨打电话

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);
  }
}

适用于模拟器2.2

答案 2 :(得分:3)

答案 3 :(得分:1)

你可以使用

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

为您设置为tabSpec内容的每个活动,每次按下选项卡时都会创建此活动

答案 4 :(得分:0)

commonsware.com是正确的,这是不可能的。我有类似的问题,但只有一项活动正在启动。我牺牲了一点我的架构并删除了从选项卡内部启动的活动。我将代码放在View中,然后将ViewAnimator添加到选项卡的活动中。我覆盖了后退按钮并移除了该视图(如果它已启动),或者让后退按钮正常运行。

这很好地伪造了它,并且对于只有一个密切相关的活动,我不会因为设计考虑而失眠。

答案 5 :(得分:0)

此问题中的2个标签栏怎么样?第一个是菜单底部标签栏,第二个是顶部标签栏,它们是不同的活动和xml

答案 6 :(得分:0)

我可以从工具栏中的微调器中打开一个片段或活动吗?

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;
>     } }