0

I am new to android development and I am trying to pass a Boolean value from dialog fragment to the activity.

the boolean value is supposed to be decided by the user(depends on which button user clicked). However, the boolean immediately turned to false without clicking any button.

I have tried various method recommended I found on internet but none of them works for me(I guess I have some part screwed up...), these method include:

-broadcasting -implementing interface -intent.putExtra

and below is the code that I have came up with, could anyone help me take a look? Any help is appreciated.

Stage:

package com.example.fuj.valorsafeworldbytrade;

import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import static com.example.fuj.valorsafeworldbytrade.LosingDialogFragment.LOSING_FRAGMENT;

public class Stage extends AppCompatActivity {

String stage;
FragmentManager fragmentManager = getSupportFragmentManager();
LosingDialogFragment losingDialogFragment = new LosingDialogFragment();
BasicInfo basicInfo = new BasicInfo();

public void setText(){
    TextView cpuReputation = (TextView) findViewById(R.id.cpu_reputation);
    TextView cpuGold = (TextView) findViewById(R.id.cpu_gold);
    TextView pGoldText = (TextView) findViewById(R.id.player_gold);
    TextView pRepText = (TextView) findViewById(R.id.player_reputation);
    cpuReputation.setText(String.valueOf(basicInfo.cRep));
    cpuGold.setText(String.valueOf(basicInfo.cGold));
    pGoldText.setText(String.valueOf(basicInfo.pGold));
    pRepText.setText(String.valueOf(basicInfo.pRep));
}

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

}

protected void onStart() {
    super.onStart();
    stage = getIntent().getStringExtra(StageChoosingMenu.STAGE);
}


public void playerChoice(View view) {
    boolean deceiveEnabled;
    switch (view.getId()) {
        case R.id.cooperate_button:
            deceiveEnabled = false;
            break;

        case R.id.deceive_button:
            deceiveEnabled = true;
            break;

        default:
            throw new  RuntimeException("Unknown Button ID");
    }
    switch (stage){
        case "xumo":
            xuMo(deceiveEnabled);
            break;

    }


}

public void xuMo(boolean playerDeceiveEnabled){
    boolean cpuDeceiveEnabled;
    cpuDeceiveEnabled = (Math.random() - basicInfo.faith > 0);

    if (cpuDeceiveEnabled){
        if (playerDeceiveEnabled){
            basicInfo.playerDvsCpuD();
            // faith changes to be amend w/ proper value, need record on the change of status
        }
        if (!playerDeceiveEnabled){
            basicInfo.playerCvsCpuD();
        }
    }

    if (!cpuDeceiveEnabled){
        if (playerDeceiveEnabled){
            basicInfo.playerDvsCpuC();
        }
        if (!playerDeceiveEnabled){
            basicInfo.playerCvsCpuC();
        }
    }
    if(basicInfo.pGold <= 0 || basicInfo.cGold <= 0 || basicInfo.pRep <= 0 || basicInfo.cRep <= 0){
        //to be changed
        setText();
        losingDialogFragment.show(fragmentManager,LOSING_FRAGMENT);
//trying to show a alert dialog fragment
        Log.d("True", String.valueOf(losingDialogFragment.tryAgainEnabled));

        if(losingDialogFragment.tryAgainEnabled){//tryAgainEnabled is always false
//This is the part that I wanted to retrieve data from the user(if they want to try again or not)
            basicInfo.reset();
            losingDialogFragment.dismiss();

        }else{
            losingDialogFragment.dismiss();
        }
    }
    setText();
        // to be changed

}
}

LosingDialogFragment:

package com.example.fuj.valorsafeworldbytrade;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
 * Created by fuj on 20/1/2017.
 */

public class LosingDialogFragment extends DialogFragment{

public static final String LOSING_FRAGMENT = "LOSING";
public boolean tryAgainEnabled;

Intent intent = new Intent();


@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.fragment_lose, container, false);;
//the following code is used to set the boolean value after the user click the button

    final Button tryAgainButton = (Button)rootView.findViewById(R.id.try_again_button);
    tryAgainButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tryAgainEnabled = true;
        }
    });

    Button giveUpButton =(Button)rootView.findViewById(R.id.give_up_button);
    giveUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tryAgainEnabled = false;
        }
    });
    getDialog().setTitle(LOSING_FRAGMENT);
    return rootView;
}
}

I am also sorry that I am not the best with English, if any things is not clear or not polite because of my poor English, please kindly let me know. I apologize in advance for any mistakes I have made.

Noobnewbier
  • 149
  • 1
  • 14

2 Answers2

0

Declare public boolean tryAgainEnabled; in your Stage Activity and youd can update that variable using ((Stage)context).tryAgainEnabled.

    package com.example.fuj.valorsafeworldbytrade;

import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import static com.example.fuj.valorsafeworldbytrade.LosingDialogFragment.LOSING_FRAGMENT;

public class Stage extends AppCompatActivity {

String stage;
FragmentManager fragmentManager = getSupportFragmentManager();
LosingDialogFragment losingDialogFragment = new LosingDialogFragment();
BasicInfo basicInfo = new BasicInfo();
public boolean tryAgainEnabled;
public void setText(){
    TextView cpuReputation = (TextView) findViewById(R.id.cpu_reputation);
    TextView cpuGold = (TextView) findViewById(R.id.cpu_gold);
    TextView pGoldText = (TextView) findViewById(R.id.player_gold);
    TextView pRepText = (TextView) findViewById(R.id.player_reputation);
    cpuReputation.setText(String.valueOf(basicInfo.cRep));
    cpuGold.setText(String.valueOf(basicInfo.cGold));
    pGoldText.setText(String.valueOf(basicInfo.pGold));
    pRepText.setText(String.valueOf(basicInfo.pRep));
}

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

}

protected void onStart() {
    super.onStart();
    stage = getIntent().getStringExtra(StageChoosingMenu.STAGE);
}


public void playerChoice(View view) {
    boolean deceiveEnabled;
    switch (view.getId()) {
        case R.id.cooperate_button:
            deceiveEnabled = false;
            break;

        case R.id.deceive_button:
            deceiveEnabled = true;
            break;

        default:
            throw new  RuntimeException("Unknown Button ID");
    }
    switch (stage){
        case "xumo":
            xuMo(deceiveEnabled);
            break;

    }


}

public void xuMo(boolean playerDeceiveEnabled){
    boolean cpuDeceiveEnabled;
    cpuDeceiveEnabled = (Math.random() - basicInfo.faith > 0);

    if (cpuDeceiveEnabled){
        if (playerDeceiveEnabled){
            basicInfo.playerDvsCpuD();
            // faith changes to be amend w/ proper value, need record on the change of status
        }
        if (!playerDeceiveEnabled){
            basicInfo.playerCvsCpuD();
        }
    }

    if (!cpuDeceiveEnabled){
        if (playerDeceiveEnabled){
            basicInfo.playerDvsCpuC();
        }
        if (!playerDeceiveEnabled){
            basicInfo.playerCvsCpuC();
        }
    }
    if(basicInfo.pGold <= 0 || basicInfo.cGold <= 0 || basicInfo.pRep <= 0 || basicInfo.cRep <= 0){
        //to be changed
        setText();
        losingDialogFragment.show(fragmentManager,LOSING_FRAGMENT);
        Log.d("True", String.valueOf(losingDialogFragment.tryAgainEnabled));

        if(losingDialogFragment.tryAgainEnabled){//tryAgainEnabled is always false
            basicInfo.reset();
            losingDialogFragment.dismiss();
        }else{
            losingDialogFragment.dismiss();
        }
    }
    setText();
        // to be changed

}
}

And in Fragment

package com.example.fuj.valorsafeworldbytrade;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
 * Created by fuj on 20/1/2017.
 */

public class LosingDialogFragment extends DialogFragment{

public static final String LOSING_FRAGMENT = "LOSING";
Context context;
Intent intent = new Intent();


@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.fragment_lose, container, false);;

    final Button tryAgainButton = (Button)rootView.findViewById(R.id.try_again_button);
    tryAgainButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((Stage)context).tryAgainEnabled = true;
        }
    });

    Button giveUpButton =(Button)rootView.findViewById(R.id.give_up_button);
    giveUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((Stage)context).tryAgainEnabled = false;
        }
    });
    getDialog().setTitle(LOSING_FRAGMENT);
    return rootView;
}
}
Mayur Gangurde
  • 1,552
  • 12
  • 22
0

It's because you check the value of tryAgainEnabled exactly right after showing the Dialog. Dialog starts Asynchronously, It means that it starts in diffrent Thread and your current thread doesn't wait for dismissing Dialog, So this line of your code if(losingDialogFragment.tryAgainEnabled){ runs exactly after you show dialog, befor you set value to tryAgainEnabled. Because the default value of boolean is always false, you will get false everytime.

I suggest that use listener for this:

Dialog:

public class LosingDialogFragment extends DialogFragment {

    public static final String LOSING_FRAGMENT = "LOSING";
    public boolean tryAgainEnabled;

    Intent intent = new Intent();


    private View.OnClickListener onTryAgainButtonClickLisnter;
    private View.OnClickListener onGiveUpButtonClickLisnter;
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_lose, container, false);;

        final Button tryAgainButton = (Button)rootView.findViewById(R.id.try_again_button);
        tryAgainButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if(onTryAgainButtonClickLisnter!=null)
                onTryAgainButtonClickLisnter.onClick(v);
            }
        });

        Button giveUpButton =(Button)rootView.findViewById(R.id.give_up_button);
        giveUpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onGiveUpButtonClickLisnter!=null)
                    onGiveUpButtonClickLisnter.onClick(v);
            }
        });
        getDialog().setTitle(LOSING_FRAGMENT);
        return rootView;
    }

    public void setOnTryAgainButtonClickLisnter(View.OnClickListener onTryAgainButtonClickLisnter) {
        this.onTryAgainButtonClickLisnter = onTryAgainButtonClickLisnter;
    }

    public void setOnGiveUpButtonClickLisnter(View.OnClickListener onGiveUpButtonClickLisnter) {
        this.onGiveUpButtonClickLisnter = onGiveUpButtonClickLisnter;
    }
}

In your activity call dialog like this:

LosingDialogFragment losingDialogFragment = new LosingDialogFragment();
    losingDialogFragment.setOnTryAgainButtonClickLisnter(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tryAgain();
            losingDialogFragment.dismiss();
        }
    });
    losingDialogFragment.setOnGiveUpButtonClickLisnter(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            giveUp();
            losingDialogFragment.dismiss();
        }
    });
losingDialogFragment.show(fragmentManager, "");
Saman Salehi
  • 1,995
  • 1
  • 22
  • 36