【问题标题】:How to intent from a Fragment Class to an Actiivity class and from an Activity class to a Fragment class如何从 Fragment 类到 Activity 类以及从 Activity 类到 Fragment 类
【发布时间】:2018-09-30 04:00:27
【问题描述】:
@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();

    if(result.contains("login success")){

        Intent intent = new Intent(context, Viewlpost.class);
        context.startActivity(intent);         
    }
}

这是我的代码,用于将用户从 loginactivty 重定向到片段活动

此语法不起作用,因为应用程序在执行意图后崩溃。

我还需要一个片段内的按钮,单击该按钮需要重定向到另一个片段。帐户以编辑帐户。 这是代码

changepword.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            Intent intent = new Intent(getActivity(),EditAccount.class);
            startActivity(intent);
        }
    });

【问题讨论】:

  • 您想将数据从活动发送到片段吗?在这种情况下,您将不得不使用捆绑中的数据。 Intent 仅用于在两个活动之间发送数据。

标签: android android-studio android-fragments


【解决方案1】:

要将数据从一个活动传递到另一个活动,您必须使用以下内容:

Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("key",data);
startActivity(intent);

要传递给片段,请使用以下代码行:

Bundle bundle = new Bundle();
bundle.putExtra("key",data); 

 FragmentManager fragMan = getFragmentManager();
 FragmentTransaction fragTransaction = fragMan.beginTransaction(); 
 YourFragment fragment = new YoutFragment();
 fragment.setArguments(bundle);
 fragTransaction.add(R.id.fragmentLayout, fragment);
 fragTransaction.commit();

【讨论】:

    【解决方案2】:

    从片段到活动

                    Intent intent = new Intent(getActivity(),ViewLPost.class);
                    startActivity(intent);
    

    从一个活动到一个片段,它应该是你想要回去的,所以finish();足以完成任务

    【讨论】:

    【解决方案3】:

    片段不像活动那样通过 Intent 被调用。它们只能作为 Activity 的一部分存在,这就是它们的设计目的

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(YourFragment.newInstance(), null);
    ft.commit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      相关资源
      最近更新 更多