sending data from activity to fragment - android

I have a side menu, when the side menu is pressed. It should take the SharedPreferences String and send it to the Fragment. And the Fragment should set the TextView and it will be displayed on the MainActivity.
The sent data to fragment is not showing up in the MainActivity,I am getting the error. Any solutions?
Main Activity code (when option pressed):-
if(num == 0)
{
SharedPreferences sp = getSharedPreferences("Login", Context.MODE_PRIVATE);
username = sp.getString("username", "DEFAULT");
FragmentOne F1 = new FragmentOne();
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
FT.add(R.id.relative_main, F1);
F1.setTextV(username);
FT.commit();
}
Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
View v = inflater.inflate(R.layout.fragment_one, container, false);
return v;
}
public void setTextV(String username)
{
textV.setText(username);
}

Following issue is in current implementation :
1. If fragment_one is in layout which return in from onCreateView then use v to call findViewById :
View v = inflater.inflate(R.layout.fragment_one, container, false);
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
return v;
2. Call setTextV after FT.commit(); :
FragmentTransaction FT = FM.beginTransaction();
FT.add(R.id.relative_main, F1);
FT.commit();
F1.setTextV(username);
Because you are getting value from SharedPreferences to show in TextView so get value from Preferences in onCreateView and show in TextView.

In this particular case, you don't need to send data to the fragment because in a fragment you can access to SharedPreferences.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
View v = inflater.inflate(R.layout.fragment_one, container, false);
SharedPreferences sp = getActivity().getSharedPreferences("Login", Context.MODE_PRIVATE);
username = sp.getString("username", "DEFAULT");
textV.setText(username);
return v;
}
For other cases, the best way to send information from the Activity to the Fragment is using a Bundle.
if(num == 0)
{
SharedPreferences sp = getSharedPreferences("Login", Context.MODE_PRIVATE);
username = sp.getString("username", "DEFAULT");
FragmentOne F1 = new FragmentOne();
Bundle bundle = new Bundle();
bundle.putString("username", username);
// You can add all the information that you need in the same bundle
F1.setArguments(bundle);
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
FT.add(R.id.relative_main, F1);
FT.commit();
}
Fragment:-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
View v = inflater.inflate(R.layout.fragment_one, container, false);
textV.setText(getArguments().getString("username", ""));
return v;
}

Related

How to use a single webView for multiple buttons in fragments

I am developing my android application (Java) in android studio i am new to this.
there are two fragments A & B,
fragment A contains multiple buttons (different buttons with different links) and
fragment b contain webview.
I tried but the application is crashing when click the button.
Fragment A code
String[] urls=new String[6];
Bundle bundle;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_external_links, container, false);
LinearLayout fb_link = (LinearLayout) view.findViewById(R.id.fb_link_icon);
fb_link.setOnClickListener(this);
urls[0] ="https://facebook.com";
LinearLayout google_link = (LinearLayout) view.findViewById(R.id.google_link_icon);
google_link.setOnClickListener(this);
urls[1] ="https://google.com";
LinearLayout hotmail_link = (LinearLayout) view.findViewById(R.id.hotmail_link_icon);
hotmail_link.setOnClickListener(this);
urls[2] ="https://hotmail.com";
LinearLayout yahoo_link = (LinearLayout) view.findViewById(R.id.yahoo_link_icon);
yahoo_link.setOnClickListener(this);
// urls[3] ="https://yahoo.com";
return view;
}
#Override
public void onClick(View v) {
Fragment fragment = null;
switch (v.getId()){
case R.id.fb_link_icon:
fragment = new WebViewFragment();
bundle.putString("links" , urls[0]);
fragment.setArguments(bundle);
replaceFragment(fragment);
break;
case R.id.google_link_icon:
fragment = new WebViewFragment();
bundle.putString("links" , urls[1]);
fragment.setArguments(bundle);
replaceFragment(fragment);
break;
case R.id.hotmail_link_icon:
fragment = new WebViewFragment();
bundle.putString("links" , urls[2]);
fragment.setArguments(bundle);
replaceFragment(fragment);
break;
}
}
public void replaceFragment(Fragment Fragment){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, Fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
fragment B code
WebView webview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_web_view, container, false);
webview.findViewById(R.id.webview_frame);
Bundle bundle = this.getArguments();
String website = bundle.getString("links");
String webSite = getActivity().getIntent().getExtras().getString("links");
webview.setWebViewClient(new WebViewClient());
webview.loadUrl(website);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
return view;
}
}

How to transfer from one fragment to another in Android

I want to transfer some data like "editText1Double" and "sum" to another fragment (SecondFragment) that will use those data. How would I do that?
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_fragment, container, false);
editText1 = (EditText) v.findViewById(R.id.editText1);
editText2 = (EditText) v.findViewById(R.id.editText2);
editText3 = (EditText) v.findViewById(R.id.editText3);
textView = (TextView) v.findViewById(R.id.textView);
calculateButton = (Button) v.findViewById(R.id.calculateButton);
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1Double = Double.parseDouble(editText1.getText().toString());
editText2Double = Double.parseDouble(editText2.getText().toString());
editText3Double = Double.parseDouble(editText3.getText().toString());
sum = editText1Double + editText2Double + editText3Double;
}
});
1) Using Bundle :
FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B();//receiving fragment
Bundle bundle = new Bundle();
bundle.putDouble("key", sum);
fragment.setArguments(bundle);
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();
and Receive as
Bundle bundle = getArguments();
Double sum = bundle.getDouble("key");
2)Using Construcor:
FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B(sum);//receiving fragment
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();
and Receive as
public class Fragment_B extends Fragment{
private double sum;
public Fragment_B (double sum) {
this.sum= sum;
}
public Fragment_B () {
}}
Pass data by using
fragment = new HomeFragment();
Bundle bundle = new Bundle();
bundle.putDouble("double", 1.1);
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment, "HomeFragment").addToBackStack(null).commit();
And get data in another Fragment using
Bundle bundle = getArguments();
double d = bundle.getDouble("double")
Hope it works fine for you.
Sending data from fragment_A
FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B();//receiving fragment
Bundle bundle = new Bundle();
bundle.putDouble("sum_key", sum);
fragment.setArguments(bundle);
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();
Receiving data at fragment_B
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = getArguments();
Double sum = bundle.getDouble("sum_key");
}

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;
}

Getting NullPointerException while passing string between two Fragments [duplicate]

This question already has answers here:
How to pass values between Fragments
(18 answers)
Closed 6 years ago.
I have Two Dynamic Fragments associated with one activity, I am trying to pass one Text from First Fragment to Second Fragment using Bundle, but I am getting Null Pointer Exception. Is it the right way to pass String between two fragments? Below is my code :
First Fragment
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_main, container, false);
String text = "GetThisStringInSecondFragment";
TextView txtView = null;
txtView = (TextView) view.findViewById(R.id.firstfragmenttext);
txtView.setText(text);
Bundle bundle = new Bundle();
bundle.putString("HI", text);
return view;
}
}
Second Fragment
public class SecondFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_secondmain,
container, false);
TextView txtView = null;
txtView = (TextView) view.findViewById(R.id.secondfragmenttext);
Bundle bundle = this.getArguments();
String myInt = bundle.getString("HI");
txtView.setText(myInt);
return view;
}
}
Activity
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLoad = (Button) findViewById(R.id.btn_load);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FirstFragment hello = new FirstFragment();
fragmentTransaction.add(R.id.fragment_container, hello, "HELLO");
fragmentTransaction.commit();
FragmentManager fragmentManager2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
SecondFragment hello2 = new SecondFragment();
fragmentTransaction2.add(R.id.fragment_container, hello2, "HELLO");
fragmentTransaction2.commit();
}
};
btnLoad.setOnClickListener(listener);
}
}
You are doing getArguments in second fragment but where are you sending those arguments from first fragment. You can use Interface here and define it in activity. From activity, you can use setArgument() method for second fragment.
check the following link for more detailed information and steps to do that.
https://developer.android.com/training/basics/fragments/communicating.html

Use single Fragment for multiple UI tasks?

I am using the Fragment master detail architecture
All Fragment Classes have the same logic
Is it possible to "detach" the Fragment from the layout and use just one Fragment class
So this code:
FragmentTransaction fragManager = getSupportFragmentManager().beginTransaction();
if("001".equalsIgnoreCase(id)){
arguments.putString(Fragment001.ARG_ITEM_ID, id);
Fragment001 fragment = new Fragment001();
fragment.setArguments(arguments); fragManager.replace(R.id.item_detail_container, fragment);
}
else if("002".equalsIgnoreCase(id)){
arguments.putString(Fragment002.ARG_ITEM_ID, id);
Fragment002 fragment = new Fragment002();
fragment.setArguments(arguments);
fragManager.replace(R.id.item_detail_container, fragment);
}
fragManager.commit();
Will turn into Something like:
FragmentTransaction fragManager = getSupportFragmentManager().beginTransaction();
GenericFragment fragment = new GenericFragment();
fragment.setUiId(id)
fragManager.replace(R.id.item_detail_container, fragment);
fragManager.commit();
List item
yes its possible, you can check and select which layout to use in onCreateView...
public static final String ARG_ITEM_ID1 = "fragment001";
public static final String ARG_ITEM_ID2 = "fragment002";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
String id = "fragment001";
if(ARG_ITEM_ID1.equalsIgnoreCase(id)){
rootView = inflater.inflate(R.layout.fragment_1_layout, container, false);
}
else {
rootView = inflater.inflate(R.layout.fragment_2_layout, container, false);
}
return rootView;
}

Categories

Resources