我正在研究如何通过配置传递和保存更改大量数据。
基本上我有ActivityA
(持有FragmentA)和ActivityB
(持有FragmentB
)。目标是将一个非常大的字符串从FragmentA
传递到FragmentB
。
使用以下方法创建字符串:
public class StringHelper {
public static String generateRandomLongString() {
Random rand = new Random();
StringBuilder builder = new StringBuilder();
builder.append(rand.nextInt(1000));
builder.append("_");
for (int i = 0; i < 10000000; i++) {
builder.append((char) (rand.nextInt('z' - ' ') + ' '));
}
return builder.toString();
}
}
我尝试按照以下步骤操作:
测试不同的解决方案我注意到以下情况(在我的设备和模拟器中):
重要提示:每段代码中只显示相关代码
1。将数据传递给ActivityB:
Intent intent = new Intent(v.getContext(), ActivityB.class);
String fakedData = StringHelper.generateRandomLongString();
Log.e("test", "Launching ActivityB, data = " + fakedData);
intent.putExtra("test", fakedData);
startActivity(intent);
ActivityB:
public class ActivityB extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Log.e("Received "," "+getIntent().getStringExtra("test"));
}
}
这导致系统失败,因为捆绑超过了IPC的最大大小。
java.lang.RuntimeException: Failure from system
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1514)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:813)
at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:871)
at android.support.v4.app.Fragment.startActivity(Fragment.java:916)
这应该不是问题,因为还有其他方法可用于将数据传递给活动,共享内存,单例,静态类等(有关详细信息,请参阅THIS_LINK)
2。将数据传递给FragmentB:
使用Fragment参数我不会崩溃并且数据正确到达
public class ActivityA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
Fragment fragment;
//fragment = new MainActivityFragment();
String fakedData = StringHelper.generateRandomLongString();
Log.e("TEST", "Activity String=" + fakedData);
fragment = FragmentB.newInstance(fakedData);
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content,fragment, "fragmentTag").commit();
}
}
}
public class FragmentB extends Fragment {
public static FragmentB newInstance(String data) {
FragmentB result = new FragmentB();
Bundle args = new Bundle();
args.putString("data", data);
result.setArguments(args);
return result;
}
....
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.e("TEST", "DATA_FROM_ARGS=" + getArguments().getString("data"));
}
}
第3。在FragmentB中保存实例状态:
有趣的FragmentB可以修改数据,并且您希望保留更新的数据。似乎没有崩溃也能正常工作。
public class FragmentB extends Fragment {
private String myData;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState == null) {
myData = getArguments().getString("data");
} else {
myData = savedInstanceState.getString("savedData");
}
Log.e("TEST", "DATA=" + myData);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("savedData", myData);
}
}
问题