I want to pass intent value to fragment, right now in the activity i am getting the intent value through onNewIntent but i am passing the intent value through Bundle to Fragment but it's not working.
* MainActivity.java *
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
parseMessage = intent.getStringExtra("message");
if (parseMessage.length()>0) {
Bundle bundle = new Bundle();
bundle.putString("parseJson", parseMessage);
AbcdFragment activityFrag = new AbcdFragment();
activityFrag.setArguments(bundle);
}
}
@SuppressLint("DefaultLocale")
public class AllPagerAdapter extends FragmentPagerAdapter {
public AllPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return AbcdFragment.newInstance(getApplicationContext());
case 1:
return HelloFragment.newInstance(getApplicationContext());
default:
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position].toUpperCase();
}
@Override
public int getCount() {
return 2;
}
}
* AbcdFragment.java *
public class AbcdFragment extends Fragment{
public static AbcdFragment newInstance(Context context) {
AbcdFragment fragment = new AbcdFragment();
fragment.context = context;
return fragment;
}
@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.abcd_activity,container,false);
try {
parseJsonRes = getArguments().getString("parseJson");
Log.d("ParseActivityMsg", parseJsonRes);
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
}
It's a viewpager fragment i want pass the intent value to AbcdFragment but i am getting null value in AbcdFragment.
Please kindly go through my code and suggest me some solution
Take a look at how you are creating fragment in the view pager return AbcdFragment.newInstance(getApplicationContext());
and then look at your implementation of AbcdFragment.newInstance
public static AbcdFragment newInstance(Context context) {
AbcdFragment fragment = new AbcdFragment();
fragment.context = context;
return fragment;
}
In return AbcdFragment.newInstance()
you have to set the arguments for the fragment that you create inside that method. Also, you aren't even using the fragment you created in onNewIntent, you are just creating it and doing nothing with it.
Also, chances are if onNewIntent is being called, MainActivity could already active so AbcdFragment would probably already be in memory, therefore getItem wouldn't be called and a newInstance wouldn't be created with the new arguments, if that makes sense. Because you only have 2 fragments in your ViewPager, once they are created for the first time they will be stored in memory instead of being recreated each time you swap between the two.
So, if you want to persist with your current implementation, you have to set the arguments in AbcdFragment.newInstance
and ensure that a new instance of the fragment is created AFTER onNewIntent. This probably isn't the best way to get data to your fragment.