-1

In my app I have three fragments. In the third fragment, a variable is take from a seekBar. Now I want to use this variable in my MainActivity. I tried to send the variable with an intent and show it in a textView onClick to test it, but the textView only shows „null“. Why isn‘t the variable send to the activity?

My MainActivity:

public class MainActivity extends AppCompatActivity {

    TextView textTest;
    public int a = 33;

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

        textTest = (TextView) findViewById(R.id.textView3);

        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent intent = getIntent();
                String messages = intent.getStringExtra("message");
                textTest.setText(String.valueOf(messages));
            }
        });
    }
}

My Fragment that sends the variable:

public class ItemThreeFragment extends Fragment {

    SeekBar seekBar;
    TextView textView11;
    int value = 10;

    public static ItemThreeFragment newInstance() {
        ItemThreeFragment fragment = new ItemThreeFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_item_three, container, false);

        seekBar = (SeekBar) view.findViewById(R.id.seekBar);
        seekBar.setMax(25);
        seekBar.setProgress(value);

        textView11 = (TextView) view.findViewById(R.id.textView11);
        textView11.setText("" + value);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                value = i;
                textView11.setText("" + value);

                Intent intent = new Intent(getActivity().getBaseContext(),
                        MainActivity.class);
                intent.putExtra("message", value);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        }) 
        return view;
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mike Kng
  • 255
  • 2
  • 11
  • 1
    Possible duplicate of [How to declare global variables in Android?](https://stackoverflow.com/questions/708012/how-to-declare-global-variables-in-android) – kpie Jun 06 '18 at 10:52
  • You can use Interface to communicate with fragment to activity – Amit Ranjan Jun 06 '18 at 12:57

3 Answers3

2

The problems are

In your activity, Intent intent = getIntent(); will get the intent that starts MainActivity.

In your fragment's onProgressChanged, your code doesn't communicate with MainActivity at all.

I can think of two relatively simple solutions for now:

  1. Call a MainActivity function using ((MainActivity) getActivity()).someFunction() in your fragment.

  2. In the MainActivity, use ((YourFragment) getSupportFragmentManager().findFragmentById(R.id.your_fragment_id)).property to access the fragment object's content. You can put the seek bar value into a class variable.

And check this: Communicating between a fragment and an activity - best practices

Dewey Reed
  • 4,353
  • 2
  • 27
  • 41
  • Okay, thank you so far. Can you tell me how Iwould access a static variable in MainActivity from fragment? – Mike Kng Jun 06 '18 at 11:27
  • It seems like a good idea to access the content from MainActivity like this, but do you know what‘s the fragment id? – Mike Kng Jun 06 '18 at 13:17
  • @MikeKng According to the code you posted, it seems you use in the layout XML. So simply add an id to it. Same as assigning id to a view. If you add fragments programmatically, you can add a tag during adding and use `findFragmentByTag` instead. – Dewey Reed Jun 06 '18 at 13:41
  • I add them programmatically, I left the part out in the code above. I passed a tag while creating the fragment and called it, but my compiler cannot resolve the ..().property; . Do you have another suggestion how I can pass my variable? – Mike Kng Jun 06 '18 at 14:15
  • @MikeKng try to replace "property" with "value" since you put SeekBar's value to "value" property. Make "value" public if compiler still cannot resolve. – Dewey Reed Jun 07 '18 at 02:58
0

getActivity().findViewById(R.id.textView3).setText(""+value); put the code inside onProgressChanged(),I hope its help you.

kundan kamal
  • 674
  • 7
  • 16
-1

I think you missed startActivity(intent).

 Intent intent = new Intent(getActivity().getBaseContext(),
                        MainActivity.class);
                intent.putExtra("message", value);
                 getActivity().startActivity(intent);
anothernode
  • 5,100
  • 13
  • 43
  • 62
geek919
  • 317
  • 3
  • 11