1

My question is relative to this question Clearing intent but I'm having problems implementing it.

My first class TodaysExercise.java has a button, when the button is clicked I putExtra intent.putExtra("highlegs", "High Legs"); and startActivity(intent);

TodaysExercise.java

final Intent intent = getIntent();
    if (intent != null) {
        String clicked = intent.getStringExtra("button");

        if (clicked.equals("btn1")) {

            intent.setClass(TodaysExercise.this, DoExercise.class);

            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //When btn1 is clicked I putExtra
                    intent.putExtra("highlegs", "High Legs");
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);

                }
            });


        }
    }

The next class DoExercise.java then get the string String text = bundle.getString("highlegs"); and set it to the textView textView.setText(text);. I then check if TextView is equal to if (textView.getText().equals("High Legs")) and if it is, I once again putExtra i.putExtra("next", "Leg Curls X 20"); and start next class startActivity(intent);

DoExercise.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_do_the_exercise);
    textView = (TextView) findViewById(R.id.replace_this);
    descheader = (TextView) findViewById(R.id.desc_header_id);
    fab = (FloatingActionButton) findViewById(R.id.fab);

    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
    descheader.setTypeface(custom_font);


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

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        String text = bundle.getString("highlegs");


        if (text != null)
            textView.setText(text);

    }

    if (textView.getText().equals("High Legs")) 
    {

        ImageView imLoading = (ImageView) findViewById(R.id.loadingView);
        imLoading.setBackgroundResource(R.drawable.workout);
        AnimationDrawable frameAnimation = (AnimationDrawable) imLoading
                .getBackground();
        frameAnimation.start();


        final Intent i = new Intent();
        i.setClass(DoExercise.this, ReadyForNext.class);
        fab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                i.putExtra("next", "Leg Curls X 20");
                startActivity(i);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

            }
        });


    }

I get String text = bundle.getString("next"); and set tv_nextdesc.setText(text); the string extras to the TextView. I also have a button in this class that should return to DoExercise.java and this is where my question is.

ReadyForNext.java

public class ReadyForNext extends AppCompatActivity {
Button btn_next_exercise;
TextView tv_nextdesc;
TextView tv_nexttxt;
Context f;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ready_for_next);
    btn_next_exercise = (Button) findViewById(R.id.btn_next_exercise);
    tv_nextdesc = (TextView) findViewById(R.id.tv_nextdesc);
    tv_nexttxt = (TextView) findViewById(R.id.nxtTxt);

    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
    tv_nextdesc.setTypeface(custom_font);
    tv_nexttxt.setTypeface(custom_font);


    Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String text = bundle.getString("next");
            if (text != null)
                tv_nextdesc.setText(text);

        }


    final Intent i = new Intent();
    i.setClass(ReadyForNext.this, DoExercise.class);
    btn_next_exercise.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //I first check what the title is to determine what to put extra
            if (tv_nextdesc.getText().equals("Leg Curls X 20")) {
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                //then here I putExtra
                i.putExtra("legcurls", "Leg Curls");
                startActivity(i);
            }
        }
    });

}

So as you can see I have 3 classes that basically run in a circle, TodaysExercise.java opens DoExercise.java and it opens ReadyForNext.java from here I want to open DoExercise.java again (Reuse DoExercise.java) but that is when I want to clear the intent to put a new intent extra?

Any help on how to achieve this?

Community
  • 1
  • 1
Brooks
  • 31
  • 8
  • Your activities are realted as: A-> B-> C and then you wants to remove C and update Activity B. Suggestion 1: If these three screen are combinely responsible for a single task, it is better to use fragment instead of Activity. It will be more easy to handle such conditions with Fragments. Suggestion 2: While moving from B-> C make call to this.finish(); in Activity B. And from Activity C fire a new fresh intent to C with new set of bundle. – nnn Feb 08 '17 at 07:49
  • The problem is that in "B"-"DoExercise.java" I `String text = bundle.getString("highlegs");` and `textView.setText(text);` so when I return to that activity from "C"-"ReadyForNext.java" it calls it again. I need a way to clear that intentExtra and call the new intentExtra. – Brooks Feb 08 '17 at 08:16

1 Answers1

0

Just finish your DoExercise activity here

fab.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            i.putExtra("next", "Leg Curls X 20");
            startActivity(i);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            finish();

        }
    });

and start from ReadyForNext activity like you're doing.

Now you have to do some changes in DoExercise class because your intent putExtra key change with different class otherwise you'll get null pointerException

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    if(bundle.containsKey("highlegs")){
    String text = bundle.getString("highlegs");
    }
    if(bundle.containsKey("legcurls")){
    String text = bundle.getString("legcurls");
    }


    if (text != null)
        textView.setText(text);

}

Hope this will help you.

Vishal Chauhan
  • 932
  • 1
  • 6
  • 11
  • But where to I getstring and settext to `legcurls`? In your code you only state if it is, meaning that it will always be `highlegs` and `legcurls` will never be called? – Brooks Feb 08 '17 at 08:33
  • I got it working, thank you. I used your code and placed `String text2 = bundle.getString("legcurls");` below `if (bundle != null) {` – Brooks Feb 08 '17 at 08:39