how to send a String data from Main-activity to fragment? - android

I have a web-view in a fragment. I want to load the webview by selecting a url from the main activity. For example my webview is loading the url http://google.com/ ,which is from (url) my main activity. In other words, I have a string like below in my main activity , I want to use it to load my web-view which is in the fragment , how can I do that?
Please help me to solve this problem
String url="http://google.com/";

Use this to send data to fragment from activity
Bundle bundle=new Bundle();
bundle.putString("url", "http://google.com/");
//set Fragmentclass Arguments
Fragmentclass fragobj=new Fragmentclass();
fragobj.setArguments(bundle);
And then recieve in fragment's onCreateView method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext=getArguments().getString("url");
return inflater.inflate(R.layout.fragment, container, false);
}

You can do that by two ways.
1) By making the setter method for url.
public class WebViewFragment extends Fragment {
private String url;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
return rootView;
}
public void setUrl(String url){
this.url = url;
}
}
Call that methord before adding fragment:
WebViewFragment fragment = new WebViewFragment();
fragment.setUrl(url);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container,fragment).commit();
2) You can use that bu intent bundle
Bundle bundle = new Bundle();
bundle.putString("url", "www.google.com");
Fragment fragment = new Fragment();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
Parse it in fragment:
public class WebViewFragment extends Fragment {
private String url;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
url = getArguments().getString("url");
return rootView;
}
}

try this:
Fragment homeFragment = new LotteryDetailFragment();
FragmentTransaction homeTransaction = getFragmentManager().beginTransaction();
String str = "you string";
Bundle bundle = new Bundle();
bundle.putString("param_str", str);
homeFragment.setArguments(bundle);
homeTransaction.addToBackStack("HomeFragment");
homeTransaction.replace(R.id.frame_container, homeFragment, "HomeFragment");
homeTransaction.commit();
in on onCreateView of destination fragment
String str=getArguments().getString("param_str");

Try this :
1 - From Activity :
Bundle bundle = new Bundle();
bundle.putString("url", "www.google.com");
Fragment fragment = new YourFragment();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
2 - In Fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layoutView = inflater.inflate(R.layout.your_layout, container, false);
this.activity = getActivity();
String url = getArguments().getString("url");
return layoutView;
}

Try
Send data from Activity
Bundle data = new Bundle();
data.putString("url", "http://www.google.com");
MyFragment frg = new MyFragment();
frg.setArguments(data);
Receive data in Fragment
String url = getArguments().getString("url");

Related

how to pass data from activity to fragment in android without budle?

This is the activity class of my project
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs_activity);
String LicID = "DATA"
Fragment FragmentDetail = new FragmentDetail();
Bundle data = new Bundle();
data.putString("data",LicID);
FragmentDetail.setArguments(data);
ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager());
adapter.AddFragment(new FragmentDetail(),"Detail");
viewp.setAdapter(adapter);
tablay.setupWithViewPager(viewp);
}
}
This is my fragmentDetail activity. and it is a tab fragment...
public class FragmentDetail extends Fragment {
public FragmentDetail() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.detail_fragment,container,false);
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String LicID = getArguments().getString ("data");
Toast.makeText(getContext(),""+LicID+" ",Toast.LENGTH_SHORT).show();
}
This code is not work.app crash without any error.... please help. thank you
in tab_activity:
public String LicID = "DATA"
When you want to access this variable from the fragment attached to activity,
in Detail Fragment :
((tab_activity) (getActivity)).LicID
so you don't have to pass arguments to the fragment and use bundle for this. If you need further assistance, please show your logcat
I think the problem is that you set argument to one fragment:
Fragment FragmentDetail = new FragmentDetail();
Bundle data = new Bundle();
data.putString("data",LicID);
FragmentDetail.setArguments(data);
And add to adapter another fragment:
adapter.AddFragment(new FragmentDetail(),"Detail");
Please, try to use the same fragment.

How to send bundle from baseAdapter to a fragment?

Actually i am sending bundle from my base adapter to a new fragment. how I send bundle from adapter to frament.
here is my adapter code:
textViewItemName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ExpandableCategoryList fragment = new ExpandableCategoryList();
Bundle bundle = new Bundle();
bundle.putInt("Id",Id);
fragment.setArguments(bundle);
((MainActivity)context).replaceFragment(fragment,
"TAG",null,false);
//Log.e("Hello",fragment.toString());
}
});
and i am receiving mu bundle as
int id = getArguments().getInt("Id",0);
but i am getting that getArguments().getInt() throws null point exception. How I handle it
In your Adapter, write the code
ExpandableCategoryList fragmentProductLaptop = new ExpandableCategoryList();
FragmentTransaction fragmentTransaction=context.getSupportFragmentManager().beginTransaction();
Bundle args = new Bundle();
args.putString("Name",category.getCategoryName());
args.putInt("Id",category.getId());
fragmentProductLaptop.setArguments(bundle);
fragmentTransaction.replace(R.id. homeFrameLayout, fragmentProductLaptop);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
In your Fragment Class, write the below code :
public class ExpandableCategoryList extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String name = getArguments().getString("Name");
String Id = getArguments().getInt("Id",0);
return inflater.inflate(R.layout.fragment, container, false);
}
}

Passing data by using bundle between tablayout

I am using Bundle to passing data from my First TabFragment but it prompts out with the NullPointerException. Error is occur when getArguments() in list_fragments2 in second tab
MainActivity Fragment
list_fragment2 fragment = new list_fragment2();
Bundle b = new Bundle();
b.putString("test","text");
fragment.setArguments(b);
Toast.makeText(this, "" + b, Toast.LENGTH_SHORT).show();
SecondActivity Fragment
public class list_fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab2, container, false);
TextView textView = (TextView) h.findViewById(R.id.textView);
Bundle bundle=getArguments();
//your string
if(bundle != null) {
String test = bundle.getString("test");
textView.setText(test);
}
return h;
}
}
Do you actually load your second fragment?
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
list_fragment2 fragment = new list_fragment2();
Bundle b = new Bundle();
b.putString("test","text");
fragment.setArguments(b);
fragmentTransaction.replace(placeholder, fragment);
fragmentTransaction.commit();
I think you can create your instance of list_fragment2 code within list_fragment2. Maybe your codes recreated list_fragment2 many times.
public static list_fragment2 createInstance() {
list_fragment2 fragment = new list_fragment2();
Bundle bundle = new Bundle();
bundle .putString("test","text");
fragment.setArguments(bundle);
return fragment;
}

how pass parameters between two fragment?

i must pass two parameters from fragment to another fragment. First parameter is type String and second parameter is type int. i want pass parameters with bundle but doesn't work.
FIRST FRAGMENT
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt("totale", totalAmount);
fragment.setArguments(bundle);
SECOND FRAGMENT
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
TextView titolo2 = (TextView) getView().findViewById(R.id.quantità2);
Bundle bundle=this.getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(myInt);
return inflater.inflate(R.layout.fragment_two, container, false);
}
Change Second Fragment like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_two, container, false);
TextView titolo2 = rootView.findViewById(R.id.quantità2);
Bundle bundle=this.getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(myInt + "");
return rootView;
}
In first fragment write this:
TwoFragment fragment = new TwoFragment ();
Bundle bundle = new Bundle();
bundle.putInt("totale", totalAmount);
fragment.setArguments(bundle);
Second fragmnent:
Bundle bundle=getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(myInt);
Create a public class with two static members like this :
public class SomeClass {
public static String stringValue="";
public static int integerValue=0;
}
Then from your first fragment just set values like this :
SomeClass.stringValue="yourValue";
SomeClass.integerValue=yourIntegerValue;
You can use these variable from your second fragment where you want :
int a =SomeClass.integerValue;
String str=SomeClass.stringValue;
If both fragments belong to the same activity, better to use this approach:
In activity you should save references to fragments, and all comunications between this fragments should be through activity, let's name it BaseActivity. For example add to activity method which will pass params to second fragment:
void updateSecondFragment(int param1, String param2){
mFragment2.updateTitle(param2);
}
In second fragment:
void updateTitle(String title){
mTextView.setText(title);
}
And in first fragment:
BaseActivity mActivity;
#Override
void onAttach(Activity activity) {
if (activity instanceOf BaseActivity){
mActivity = (BaseActivity) activity;
}
}
private void updateSecondFragment(){
if (mActivity != null){
mActivity.updateSecondFragment(int param1, String param2);
}
}
You should do the work of second fragment as:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView titolo2 = view.findViewById(R.id.quantità2);
Bundle bundle=getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(String.valueOf(myInt));
}

Send two strings from fragment to fragment

I don't understand how to transfer data between two fragments. I've tried this:
public class Connection extends Fragment
{
SendMessage sender;
public interface SendMessage
{
public void send(String one, String two);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//my data
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
send = (SendMessage) getActivity();
}
private class AlarmReceiver extends BroadcastReceiver
{
public void onReceive(final Context context, Intent intent)
{
if(message.equals("POS"))
{
sender.send(position,id);
}
}
}
in the first fragment; in the MainActivity:
public class MainActivity extends FragmentActivity implements Connection.SendMessage
{
protected void onCreate(Bundle savedInstanceState)
{
//my code
}
#Override
public void send(String one, String two)
{
Map map = new Map();
final Bundle bundle = new Bundle();
bundle.putString("Position",one);
bundle.putString("ID:",two);
map.setArguments(bundle);
}
}
and in the Map fragment(it need to receive this data):
public class Map extends Fragment
{
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Bundle bundle = new Bundle();
bundle.getArguments();
if (bundle != null)
{
String pos = bundle.getString("Position");
String id = bundle.getString("ID");
}
//then show this on googleMaps
}
}
but I don't understand why this code doesn't work..could you help me? thanks...
1) Please change your class name from Map.java to anything else. Because in java map is imported as java.util Package.
2)
Fragment map = new Fragment ();
final Bundle bundle = new Bundle();
bundle.putString("Position",one);
bundle.putString("ID:",two);
map.setArguments(bundle);
map.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, map).commit();
and in recieve
Bundle bundle = getArguments();
if (bundle != null)
{
String pos = bundle.getString("Position");
String id = bundle.getString("ID:"); (You miss : in ID)
}
Do This send fragment
String cid=id.getText().toString();
Fragment fr=new friendfragment();
FragmentManager fm=getFragmentManager();
android.app.FragmentTransaction ft=fm.beginTransaction();
Bundle args = new Bundle();
args.putString("CID", cid);
fr.setArguments(args);
ft.commit();
Received fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("CID");
return inflater.inflate(R.layout.fragment, container, false);
}
Well, firstly. Your receiving fragment is actually accessing an empty bundle. You are doing this:
Bundle bundle = new Bundle();
bundle.getArguments();
Instead of:
Bundle bundle = getArguments();
Secondly, a good way to communicate between two fragments is by using an interface. The interface communicates with the activity and the activity with the second fragment.
Here's google's tutorial:
http://developer.android.com/training/basics/fragments/communicating.html
Also, it doesn't seem like you are inflating any xml layout file for your Map Fragment or even adding it with FragmentTransaction after you have created it. Here is a short and sweet tutorial for that as well:
http://android-er.blogspot.com/2011/12/programmatically-add-fragment.html

Categories

Resources