【问题标题】:how to call Activity class from Fragment如何从 Fragment 调用 Activity 类
【发布时间】:2013-08-30 03:40:44
【问题描述】:

我正在使用 Navigation Drawer,当有人单击滑动菜单中的项目时,我想调用 testActivity.java 类。目前,当滑动菜单项时,我的 MainActivity.java 类正在调用 FragmentOne、FragmentTwo、FragmentThree selected..我想用 testActivity.java 替换它

这是我的 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();
}

}

这是我的 FragmentOne.java 类..所有其他 FragmentTwo 和 FragmentThree.java 类具有相同的代码。

      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 类

   public class testActivity extends Activity {

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


}

}

【问题讨论】:

    标签: android android-fragments android-activity slidingmenu navigation-drawer


    【解决方案1】:

    在 Fragment 中,您可以使用 getActivity() 访问其父 FragmentActivity 实例。而且由于该实例是一个活动,因此使用它您可以简单地调用另一个活动。

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

    【讨论】:

    • 你的意思是像这样在 FragmentOne 类?..但它不起作用? Intent 意图 = new Intent(getActivity(), testActivity.class); getActivity().startActivity(intent);
    • 我已经把它们像这样放了。没有错误或运行时错误。只是没有显示布局文件...... public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);查看视图 = inflater.inflate(R.layout.fragment_one,container, false); Intent 意图 = new Intent(getActivity(), testActivity.class); getActivity().startActivity(intent);返回根; }
    • 尝试将您的 getActivity 转换为 ParentActivity。我已经编辑了答案以添加示例代码。
    • 好吧.. 不要在 onCreateView() 中这样做。在 onViewCreated() 中执行此操作。可能这就是问题所在..
    • onCreateVicew() 是仍在创建视图的地方,您可能无法访问父实例。在 onViewCreated() 中,可以访问父实例。
    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 2017-05-15
    • 2017-07-20
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多