How can we call another activity by using fragment tabs?

流过昼夜 提交于 2019-12-04 06:35:34

问题


I am using fragment with tabs, and i use listfragment in tab,i want to go another acitivity on item clicklistner, i am using below code:

        Activity activity = getActivity();
        Intent i = new Intent(activity, Motherboard.class);
        startActivity(i);

it is going to another activity but it not showing tabbars. I want if any activity call then the tabbar remain infront. Please help me out.


回答1:


Hope this might helps you

http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/

and to support in all versions you can use Action bar sherlock library which is also provided in that tutorial.




回答2:


Fragments ate part of an activity. If you move to another activity and you want it to have fragments as well, you will have to create fragments in it. Alternatively, you can remain in the same activity and just switch the fragment without starting a new activity.




回答3:


I think you need to create one BaseActivity with tabs like

abstract public class BaseActivity extends FragmentActivity {

}

and all other activities extends BaseActivity

EDIT

small example for you

BaseActivity.java

 abstract public class BaseActivity extends Activity implements TabListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    ActionBar actionBar = getActionBar();
    // add tabs to actionbar
    actionBar.addTab(actionBar.newTab().setText("TAB 1")
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText("TAB 2")
            .setTabListener(this));

}

}

FirstActivity.java

   public class FirstActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    Toast.makeText(getApplicationContext(),
            tab.getText() + " selected in FirstActivity",
            Toast.LENGTH_SHORT).show();

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

}

SecondActivity.java

    public class SecondActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    Toast.makeText(getApplicationContext(),
            tab.getText() + " selected in SectondActivity",
            Toast.LENGTH_SHORT).show();

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

  }



回答4:


You need to Add/Replace Another fragment in the onclick method instead of starting another activity




回答5:


unless you do want to write a super-class that implements a tab-bar in all of your activities that inherit from this super-class, you can just try to replace your fragment with another fragment. Saves alot of memory aswell.



来源:https://stackoverflow.com/questions/18272569/how-can-we-call-another-activity-by-using-fragment-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!