我已经想从MainActivity启动我的RecipientFragment并将数据从MainActivity传递到Fragment。这是我实现的代码。但片段没有开始。
Bundle bundle = new Bundle();
bundle.putString(ParseConstants.KEY_FILE_TYPE, fileType);
RecipientsFragment keyfile = new RecipientsFragment();
keyfile.setArguments(bundle);
Fragment newFragment = new RecipientsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.commit();
我还想知道如何传递intent.setData并在Fragment中获取该数据。目前我有以下代码:
RecipientFragment
mMediaUri = getActivity().getIntent().getData();
MainActivity
Intent recipientsIntent = new Intent(this, RecipientsFragment.class);
recipientsIntent.setData(mMediaUri);
答案 0 :(得分:32)
您可以在活动中添加或替换片段。在活动布局FrameLayout
文件中创建xml
。
然后在您的活动中执行此操作以添加片段:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();
替换片段执行此操作:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();
请参阅adding a fragment to an activity上的Android文档或关于SO的相关问题:
Difference between add(), replace(), and addToBackStack()
Basic difference between add() and replace() method of Fragment
Difference between add() & replace() with Fragment's lifecycle
答案 1 :(得分:3)
首先,您有意图启动活动和服务,您可以使用片段事务启动片段。 其次,你的交易没有做任何事情。 将其更改为:
FragmentTransaction transaction = getFragmentManager();
transaction.beginTransaction()
.replace(R.layout.container, newFragment) //<---replace a view in your layout (id: container) with the newFragment
.commit();
答案 2 :(得分:1)
片段是可以在活动中显示的ViewGroup。但是它需要一个容器。容器可以是任何布局(FragmeLayout,LinearLayout等。都没有关系)。
步骤1:
定义活动布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragmentHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
第2步:
定义片段布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"/>
</LinearLayout>
第3步:
创建片段类
public class LoginFragment extends Fragment {
private Button login;
private EditText username, password;
public static LoginFragment getInstance(String username){
Bundle bundle = new Bundle();
bundle.putInt("USERNAME", username);
LoginFragment fragment = new LoginFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.login_fragment, parent, false);
login = view.findViewById(R.id.login);
username = view.findViewById(R.id.user);
password = view.findViewById(R.id.password);
String name = getArguments().getInt("USERNAME");
username.setText(username);
return view;
}
}
第4步:
在“活动”中添加片段
public class ActivityB extends AppCompatActivity{
private Fragment currentFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currentFragment = LoginFragment.getInstance("Rohit");
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragmentHolder, currentFragment, "LOGIN_TAG")
.commit();
}
}
这是非常基本的代码。如果您想在Fragment中学习更多高级主题,则可以查看以下资源:
答案 3 :(得分:1)
如何在 Android Kotlin 中从 Activity 启动片段
添加片段:
val manager: FragmentManager = supportFragmentManager
val transaction: FragmentTransaction = manager.beginTransaction()
transaction.add(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG)
transaction.addToBackStack(null)
transaction.commit()
替换片段:
val manager: FragmentManager = supportFragmentManager
val transaction = manager.beginTransaction()
transaction.replace(R.id.container, YOUR_FRAGMENT_NAME, YOUR_FRAGMENT_STRING_TAG)
transaction.addToBackStack(null)
transaction.commit()
Here is more on fragment manager: https://developer.android.com/guide/fragments/fragmentmanager
答案 4 :(得分:0)
简单方法
创建一个新的java类
public class ActivityName extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new Fragment_name_which_you_wantto_open()).commit();}
}
}
在你想要调用片段
的活动中 Intent i = new Intent(Currentactivityname.this,ActivityName.class);
startActivity(i);
另一种方法
将框架布局放置在您想要打开片段的活动中
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
将此代码粘贴到您想要打开片段
的位置 Fragment mFragment = null;
mFragment = new Name_of_fragment_which_you_want_to_open();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayout, mFragment).commit();
答案 5 :(得分:0)
为此,最好设计片段构造以接收该数据并将该数据保存在其bundle参数中。
class FragmentA extends Fragment{
public static FragmentA newInstance(YourDataClass data) {
FragmentA f = new FragmentA();
Bundle b = new Bundle();
b.putString("data", data);
f.setArguments(b);
return f;
}
}
为了从该类开始片段,您可以执行以下操作
Fragment newFragment = FragmentA.newInstance(objectofyourclassdata);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
但是,数据类必须是可仿造的或可序列化的。
有关片段的完整信息和片段使用的最佳做法,请花一些时间在官方文档上,它非常有用,至少是我的经验
https://developer.android.com/guide/components/fragments#java
答案 6 :(得分:-2)
您可以启动活动并在其上附加 RecipientsFragment ,但您无法启动片段