Search code examples
androidvariablesandroid-activityfragmentbundle

Android how to pass variables between 2 fragments in 2 different activities through intent


Right now I have 2 different activities called MainControl (with 3 fragments) and MainControl2 (with 2 fragments). I want to pass variables from 1st Fragment in Maincontrol to 1st Fragment in MainControl2.

The following is the code I am using to achieve this:-

MainControl 1st Fragment

@Override
    public void openPostDescription(Data2 data2) {
        Intent intent = new Intent(getActivity(), MainControl2.class);
        intent.putExtra("title", data2.getTitle());
        intent.putExtra("comment", data2.getNumComments());
        intent.putExtra("domain", data2.getDomain());
        intent.putExtra("author", data2.getAuthor());
        intent.putExtra("image", data2.getUrl());
        intent.putExtra("score", data2.getScore());
        intent.putExtra("ups", data2.getUps());
        startActivity(intent);
    }

MainControl2.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_control2);

        Bundle bundle = getIntent().getExtras();
        PostDescription postDescription = new PostDescription();
        postDescription.setArguments(bundle);

        toolbar = (Toolbar)findViewById(R.id.mainControl2Toolbar);
        setSupportActionBar(toolbar);



        tabLayout = (TabLayout)findViewById(R.id.mainControl2TabLayout);
        viewPager = (ViewPager)findViewById(R.id.mainControl2ViewPager);

        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        viewPagerAdapter.addFragments(new PostDescription(), "DESCRIPTION");
        viewPagerAdapter.addFragments(new PostComments(), "COMMENT");

        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

MainControl2 1st Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_post_description, container, false);

    String title = getArguments().getString("title");


    return v;
}

The error I get is title in a null object reference. I just cannot figure out at what stage is it causing this disruption, is there something I am missing?


Solution

  • In your 1st Fragment in MainControl2. write getActivity().getIntent().getStringExtra("title");

    So your final code of fragment becomes :

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_post_description, container, false);
    
        String title = getArguments().getString("title");
    
    
        return v;
    }
    

    You have used getArguments().getString("title"); Which returns the arguments passed while creating a fragment.

    know more how to use getArguments() and setArguments() here.