如何将Activity从Activity转移到片段

时间:2015-03-23 14:49:32

标签: android android-intent android-fragments

当用户更改显示名称时,我想意图返回ProfileFragment

FragmentProfile.java(Fragment) - >编辑个人资料(活动) - >保存 - > FragmentProfile.java

Intent intent = new Intent(ChangeDisplayNameActivity.this, MainActivity.class);
startActivity(intent);

** MainActivity有滑动条

2 个答案:

答案 0 :(得分:1)

如果你想将数据从活动传递到片段,从片段传递到活动,这是如何做到的:

- 从活动到片段

- 活动

MyFragment fragment = (MyFragment) f.findFragmentById(R.id.myFragment);
fragment.setData(str);

- 片段

String fromActivity = "";
public void setData(String u) {
  fromActivity = u;
}

- 从片段到活动

- 活动

 public void saveData (String u)
{
    Toast.makeText(this, "Received", Toast.LENGTH_LONG).show();
    // do something 
}

- 片段

((MyActivity)getActivity()).saveData(Res);

答案 1 :(得分:0)

使用此代码:

ProfileFragment mProfileFragment = new ProfileFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
    .beginTransaction();
fragmentTransaction.replace(R.id.content_frame,
    mProfileFragment);
fragmentTransaction.addToBackStack(MyProfileFragment.class
    .getName());
fragmentTransaction.commit();