Rexan
Rexan

Reputation: 19

How can I pass values between 2 fragments?

I want to send the values of one fragment to another fragment. My code:

Fragment 1:

    int finishedLevels;
    int skippedLevels;
    int usedHints;
    int failedLevels;

    public static final String FINISHED_LEVELS = "com.example.rexan_snerficonquiz.Quiz_Fragment.FINISHED_LEVELS";
    public static final String SKIPPED_LEVELS = "com.example.rexan_snerficonquiz.Quiz_Fragment.SKIPPED_LEVELS";
    public static final String FAILED_LEVELS = "com.example.rexan_snerficonquiz.Quiz_Fragment.FAILED_LEVELS";
    public static final String USED_HINTS = "com.example.rexan_snerficonquiz.Quiz_Fragment.USED_HINTS";



    public void openActifity2(){
        Intent intent = new Intent(getActivity().getApplication(), Quiz_Fragment.class);
        intent.putExtra(FINISHED_LEVELS, finishedLevels);
        intent.putExtra(FAILED_LEVELS, failedLevels);
        intent.putExtra(USED_HINTS, usedHints);
        intent.putExtra(SKIPPED_LEVELS, skippedLevels);
        startActivity(intent);
    }

Fragment 2:

import static com.example.rexan_snerficonquiz.Quiz_Fragment.FAILED_LEVELS;
import static com.example.rexan_snerficonquiz.Quiz_Fragment.FINISHED_LEVELS;
import static com.example.rexan_snerficonquiz.Quiz_Fragment.SKIPPED_LEVELS;
import static com.example.rexan_snerficonquiz.Quiz_Fragment.USED_HINTS;



    int finishedLevels = getActivity().getIntent().getIntExtra(FINISHED_LEVELS,0);
    int failedLevels = getActivity().getIntent().getIntExtra(FAILED_LEVELS, 0);
    int skippedLevels = getActivity().getIntent().getIntExtra(SKIPPED_LEVELS, 0);
    int usedHints = getActivity().getIntent().getIntExtra(USED_HINTS, 0);

I think I see a problem in

    Intent intent = new Intent(getActivity().getApplication(), Quiz_Fragment.class);

because the source an the destination are required. But because it is in a fragment I have to write "getActivity().getApplication()" before the class. So I use

    Intent intent = new Intent(getActivity().getApplication(), Quiz_Fragment.class,getActivity().getApplication(),Fragment_score.class);

, but this is marked as wrong. This too:

    Intent intent = new Intent(getActivity().getApplication(), Quiz_Fragment.class,Fragment_score.class);

The code above "I think I see a problem in" isnt marked as wrong. But when I use the app and the values are getting changed, the app crashes. The error code is:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.rexan_snerficonquiz, PID: 18240 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.rexan_snerficonquiz/com.example.rexan_snerficonquiz.Fragment_score}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2065) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727) at android.app.Activity.startActivityForResult(Activity.java:5314) at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676) at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234) at androidx.fragment.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:791) at androidx.fragment.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:933) at androidx.fragment.app.Fragment.startActivity(Fragment.java:1185) at androidx.fragment.app.Fragment.startActivity(Fragment.java:1173) at com.example.rexan_snerficonquiz.Quiz_Fragment.openActifity2(Quiz_Fragment.java:251) at com.example.rexan_snerficonquiz.Quiz_Fragment$2.onClick(Quiz_Fragment.java:101) at android.view.View.performClick(View.java:7438) at android.view.View.performClickInternal(View.java:7415) at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28286) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

But this doesnt make sens in my opinion. If I change

    Intent intent = new Intent(getActivity().getApplication(), Quiz_Fragment.class);

(source) to

    Intent intent = new Intent(getActivity().getApplication(), Framgent_score.class);

(destination) the error code changes everything with "Quiz_Fragment" to "Fragment_score".

Upvotes: 0

Views: 47

Answers (2)

Jide
Jide

Reputation: 54

An activity and a fragment are two different thing, you should read about the difference , an intent cannot be used to start a fragment instead a fragment manager should do the trick

FragmentManager fm= getSupportFragmentManager();
FragmentTransaction ft= fm.begin transaction();
ft.add(android.R.Id.content, yourFragment).commit();

Then you can use a singleton class to pass data between both your fragment. An example of a singleton class:

public class mysingleton{
   private static mysingleton instance= new mysingleton();

    private mysingleton() {}

   private mysingleton getInstance(){
         return instance;
   }
   // generate getter and setter for whatever data your 
    passing
}

Note: singleton classes can cause memory leaks so read on how to prevent that.

Upvotes: 0

Nour Eldien Mohamed
Nour Eldien Mohamed

Reputation: 961

you can try this

on fragment 1

 FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment2 fragment2 = new Fragment2();
        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        fragmentTransaction.replace(R.id.fragment_container, fragment2);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

on fragment 2

Bundle bundle = getArguments();
        assert bundle != null;
       String data = bundle.getString("data");

Upvotes: 1

Related Questions