如何调用从片段Activity类 [英] how to call Activity class from Fragment

查看:91
本文介绍了如何调用从片段Activity类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正与抽屉式导航栏,我想打电话给testActivity.java类,当有人点击一个项目是在滑动menu.Currently我MainActivity.java CALSS呼吁到FragmentOne,FragmentTwo,FragmentThree滑动时的菜单项selected..I希望与testActivity.java替换此

I am working with Navigation Drawer and i want to call testActivity.java class when someone click on a item that is in the sliding menu.Currently my MainActivity.java calss is calling to the FragmentOne,FragmentTwo,FragmentThree when sliding menu item selected..I want to replace this with testActivity.java

这是我的MainActivity.java

here is my MainActivity.java

 public class MainActivity extends FragmentActivity {
final String[] data ={"one","two","three"};
final String[] fragments ={
        "core.layout.FragmentOne",
        "core.layout.FragmentTwo",
        "core.layout.FragmentThree"};
@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     ArrayAdapter<String> adapter = new ArrayAdapter<String>    (getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);

     final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
     final ListView navList = (ListView) findViewById(R.id.drawer);
     navList.setAdapter(adapter);
     navList.setOnItemClickListener(new OnItemClickListener(){
             @Override
             public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                     drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                             @Override
                             public void onDrawerClosed(View drawerView){
                                     super.onDrawerClosed(drawerView);
                                     FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                                     tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
                                     tx.commit();
                             }
                     });
                     drawer.closeDrawer(navList);
             }
     });
     FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
     tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
     tx.commit();
}

}

这是我有相同的code这样的FragmentOne.java calss..All其他FragmentTwo和FragmentThree.java类。

here is my FragmentOne.java calss..All other FragmentTwo and FragmentThree.java classes having same code like this.

      public class FragmentOne extends Fragment {


public static Fragment newInstance(Context context) {
    FragmentOne f = new FragmentOne();

    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
   ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);

    View view = inflater.inflate(R.layout.fragment_one,container, false);

   return root;


}

}

这是我testActivity.java类

This is my testActivity.java class

   public class testActivity extends Activity {

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


}

}

推荐答案

在片段,您可以访问使用getActivity(),其父FragmentActivity实例。而且,由于该实例是一个活动,所以使用就可以简单地调用另一个活动。

In fragment you can access its parent FragmentActivity instance using getActivity(). And since that instance is an activity, so using that you can call another activity simply.

Intent myIntent = new Intent((ParentActivity)getActivity(), YourOtherActivity.class);
(ParentActivity)getActivity().startActivity(myIntent); 

这篇关于如何调用从片段Activity类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆