I'm building an application which receive an action.SEND Intent
and display all files in clip data into a ListView
. My ListView
is inside a ShareFragment
which hosted by my MainActiviy
. Below is how i implement it.
public class ShareFragment extends MFragment {
private static final String TAG = "ShareFragment";
private List<File> files;
private FileAdapter fileAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate called");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
files = new ArrayList<>();
fileAdapter = new FileAdapter(getActivity(), 0, files);
View rootView = inflater.inflate(R.layout.share_fragment, container, false);
ListView lv = (ListView) rootView.findViewById(R.id.listView);
lv.setAdapter(fileAdapter);
Log.e(TAG, "onCreateView called");
return rootView;
}
public void setData(List<File> files){
Log.e(TAG, "setData called");
this.files = files;
fileAdapter.notifyDataSetChanged();
}
And here is how i used it in MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
shareFragment = (ShareFragment) getSupportFragmentManager().findFragmentByTag("share_fragment");
if(shareFragment == null) {
shareFragment = new ShareFragment();
shareFragment.setTitle("Share");
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fl_root, shareFragment, "share_fragment");
ft.commit();
handleIntent(intent);
}
Here is handleIntent
method:
private void handleIntent(Intent intent) {
String action = intent.getAction();
String type = intent.getType();
if ((Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) && type != null) {
int size = intent.getClipData().getItemCount();
List<File> files = new ArrayList<>();
for (int i = 0; i < size; i++) {
File file = new File(intent.getClipData().getItemAt(i).getUri().getPath());
files.add(file);
}
shareFragment.setData(files);
}
}
My problem is When my application are started by receive an action.SEND
Intent
, it ran into onCreate
and processed this line shareFragment = new ShareFragment()
But onCreate
and onCreateView
of ShareFragment
is never called. why?
Note I thought the FragmentManager
kept an instance of ShareFragment
when my app first ran. So i try to found my fragment to reused it, but it always returned null.
EDIT I got a NullPointerException
right at fileAdapter.notifyDataSetChanged();
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void icetea.com.quickshare.adapter.FileAdapter.notifyDataSetChanged()' on a null object reference
at icetea.com.quickshare.fragments.ShareFragment.setData(ShareFragment.java:49)
at icetea.com.quickshare.MainActivity.handleIntent(MainActivity.java:63)
at icetea.com.quickshare.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
at android.app.ActivityThread.access$900(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6141)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
move handleIntent(intent);
into onResume()
you try to set the data in onCreate, but at this time the fragment isn't created
you find more about this in the Fragments documentation: http://developer.android.com/guide/components/fragments.html#Lifecycle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.