I have been trying to pass data from Main Activity to fragments using bundle. I have used TabLayout and ViewPager to add fragments on my activity in Tab format. I don't get any error but I get a null object in return and it displays "ERROR" on my 1st fragment as I have added an If condition for null object. This is my code
Main Activity Code
package com.example.activitytofragmenttest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
TextView textView;
private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Text View
textView = findViewById(R.id.mainTextView);
//TabLayout and ViewPager
tabLayout = findViewById(R.id.tabLayout);
viewPager = findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
tabViewPager();
Bundle bundle = new Bundle();
String message = textView.getText().toString();
bundle.putString("key",message);
Fragment_1 fragment_1 = new Fragment_1();
fragment_1.setArguments(bundle);
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
private void tabViewPager() {
viewPagerAdapter.AddFragment(new Fragment_1(),"Frag1");
viewPagerAdapter.AddFragment(new Fragment_2(),"Frag2");
viewPagerAdapter.AddFragment(new Fragment_3(),"Frag3");
}
}
ViewPager Code
package com.example.activitytofragmenttest;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> listFragment = new ArrayList<>();
private final List<String> listTitle = new ArrayList<>();
public ViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
return listFragment.get(position);
}
@Override
public int getCount() {
return listTitle.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return listTitle.get(position);
}
public void AddFragment (Fragment fragment, String title){
listFragment.add(fragment);
listTitle.add(title);
}
}
Fragment 1 Code
package com.example.activitytofragmenttest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Fragment_1 extends Fragment{
TextView fragment_1TextView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_1,container,false);
fragment_1TextView = v.findViewById(R.id.fragment1TextView);
Bundle message = getArguments();
if (message != null){
fragment_1TextView.setText(message.getString("key"));
}else {
fragment_1TextView.setText("Error");
}
return v;
}
}
Fragment 2 Code
package com.example.activitytofragmenttest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Fragment_2 extends Fragment {
TextView fragment_2TextView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_2, container, false);
fragment_2TextView = v.findViewById(R.id.fragment2TextView);
// Bundle bundle = getArguments();
// if (bundle != null) {
// fragment_2TextView.setText(bundle.getString("key"));
// }
return v;
}
}
Fragment 3 Code
package com.example.activitytofragmenttest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Fragment_3 extends Fragment {
TextView fragment_3TextView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_3, container, false);
fragment_3TextView = v.findViewById(R.id.fragment3TextView);
// Bundle bundle = getArguments();
// if (bundle != null) {
// fragment_2TextView.setText(bundle.getString("key"));
// }
return v;
}
}
Can anyone suggest why I get a null object from the Main Activity to fragment, and how can I transfer multiple data from the Main Activity to other fragments as well that is 2nd and 3rd fragment.
When starting the Fragment you can pass parameters through a bundle
/*ACTIVITY CODE*/
Bundle args = new Bundle();
args.putStringArrayList("argument_name", myListInString);
MyFragment fragment = new MyFragment();
fragment.setArguments(args);
fragment.show(fragmentTransaction, "fragment_tag")
And use them in your new snippet as follows
/* FRAGMENT CODE */
ArrayList<String> myList = getArguments.getStringArrayList("argument_name");
// ... FILL TABLE