简介:fragment argument无需依赖Activity的intent内的extra,Fragment就能获得自己所需要的extra数据,这样能有效的保护fragment的封装性。 一般argument是通过将数据存储在Fragment的“某个地方”,而不需要保存在Activity的私有空间,其中“某个地方”指的就是argument bundle。
- 1.每个fragment实例都可以附带一个Bundle对象。该bundle包含有键值对,我们可附加extra到Activity的intent中那样使用它们。一个键值对应一个argument。
- 2.要创建fragment argument,首先需创建Bundle对象。然后,使用Bundle限定类型的“put”方法,将argument添加到bundle中。
- 3.要附加argument bundle给fragment,调用Fragment.setArguments(Bundle)方法。而且,还必须在fragment创建后,添加给activity前完成。
- 4.通常做法:添加名为newInstance()静态方法给Fragment类。使用该方法,完成fragment实例及bundle对象的创建,然后将argument放入bundle中,在附加fragment。代码如下:
public static Fragment newInstance(String s) {
FragmentTwo fragment = new FragmentTwo();
Bundle args = new Bundle();
args.putString(ARG_VAR, s);
fragment.setArguments(args);
return fragment;
}
为了使我们对fragment argument的理解更加深入,下面通过一个简单的实例进行演示。
通过创建两个Activity和两个Fragment,一个Activity对应一个Fragment,然后实现将FragmentOne的数据传递到FragmentTwo的一个简单实例。下面贴出详细代码
fragment_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/temp_fragment_activity"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
TemplateFragmentActivity.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public abstract class TemplateFragmentActivity extends AppCompatActivity
{
private FragmentManager fm;
private FragmentTransaction ts;
private Fragment fragment;
//抽象方法,用于创建Fragment实例
protected abstract Fragment createFragment();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity);
fm = getSupportFragmentManager();
ts = fm.beginTransaction();
if (fragment == null){
fragment = createFragment();
ts.add(R.id.temp_fragment_activity,fragment);
ts.commit();
}
}
@Override
protected void onStop() {
super.onStop();
finish();
}
}
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#FDFBFB"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击下面的按钮跳转到FragmentTwoActivity"
android:textSize="20sp"
android:textAllCaps="false"
android:textColor="#F70505">
</TextView>
<EditText
android:id="@+id/et_fm_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要传递的值"
android:textSize="30sp"
android:textAllCaps="false"
android:textColor="#090808"
android:layout_marginTop="20dp">
</EditText>
<Button
android:id="@+id/btn_fm_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转并传递"
android:textSize="30dp"
android:layout_marginTop="20dp">
</Button>
</LinearLayout>
FragmentOneActivity.java
package com.example.myapplication;
import androidx.fragment.app.Fragment;
public class FragmentOneActivity extends TemplateFragmentActivity {
@Override
protected Fragment createFragment() {
return new FragmentOne();
}
}
FragmentOne.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FragmentOne extends Fragment {
private Button mBtnFragmentOne;
private EditText mEtFragmentOne;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one,container,false);
mEtFragmentOne = view.findViewById(R.id.et_fm_one);
mBtnFragmentOne = view.findViewById(R.id.btn_fm_one);
mBtnFragmentOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent intent = new Intent(getActivity(),FragmentTwoActivity.class);
Intent intent = FragmentTwoActivity.newIntent(getActivity(),
mEtFragmentOne.getText().toString());
startActivity(intent);
}
});
return view;
}
}
fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#E6D969"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="下面是fragmentOne传递到值"
android:textSize="20sp"
android:textAllCaps="false"
android:textColor="#E91E63">
</TextView>
<TextView
android:id="@+id/tv_fm_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="????????"
android:textSize="30sp"
android:textAllCaps="false"
android:textColor="#130107">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击下面的按钮跳转到FragmentOneActivity"
android:textSize="20sp"
android:textAllCaps="false"
android:textColor="#E91E63"
android:layout_marginTop="20dp">
</TextView>
<Button
android:id="@+id/btn_fm_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
android:textSize="30dp"
android:layout_marginTop="20dp">
</Button>
</LinearLayout>
FragmentTwoActivity.java
package com.example.myapplication;
import android.content.Context;
import android.content.Intent;
import androidx.fragment.app.Fragment;
public class FragmentTwoActivity extends TemplateFragmentActivity {
private static final String EXTRA_VAR =
"com.example.myapplication.android.var";
public static Intent newIntent(Context packageContext, String s) {
Intent intent = new Intent(packageContext, FragmentTwoActivity.class);
intent.putExtra(EXTRA_VAR, s);
return intent;
}
@Override
protected Fragment createFragment() {
String s = this.getIntent().getStringExtra(EXTRA_VAR);
return FragmentTwo.newInstance(s);
}
}
FragmentTwo.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FragmentTwo extends Fragment {
private static final String ARG_VAR = "var";
private Button mBtnFragmentTwo;
private TextView mTvFragmentTwo;
public static Fragment newInstance(String s) {
FragmentTwo fragment = new FragmentTwo();
Bundle args = new Bundle();
args.putString(ARG_VAR, s);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two,container,false);
String s = getArguments().getString(ARG_VAR);
mTvFragmentTwo = view.findViewById(R.id.tv_fm_two);
mTvFragmentTwo.setText(s);
//绑定按钮并实现跳转
mBtnFragmentTwo = view.findViewById(R.id.btn_fm_two);
mBtnFragmentTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),FragmentOneActivity.class);
startActivity(intent);
}
});
return view;
}
}
推荐阅读:
Android——Fragment的静态注册和动态注册
Android——从Fragment跳转到其他Activity的简单实例