samedi 11 avril 2020

Android: Why is the if condition always true or false?

I have a complicated problem and my English is not good enough for me to explain but I am going to try.

I have 3 activities: MainActivity, SecondActivity, and ThirdActivity. In SecondActivity I have a clickable ListView. In ThirdActivity I have a TextView and I paste the results with this code mantraEditText.setText(name);

I should get the clicked item results to MainActivity first then send to ThirdActivity from MainActivity. (I don't know why but only if I send from MainActivity to ThirdActivity, the app works well, if I send directly from SecondActivity to ThirdActivity I can't save the results.)

I am getting results from SecondActivity to MainActivity and send it from MainActivity to ThirdActivity with this code

        if (KEY_NAME != null){

             final Intent intent = new Intent(MainActivity.this, MainActivity.class);

                     startActivity(intent);
        }else {
            final Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    intent.putExtra(KEY_NAME, name);
                    startActivityForResult(intent, MANTRA_DETAILS_ACTIVITY_REQUEST_CODE);
                }
            });
        }

With this code when I launch the app it goes directly to ThirdActivity with empty results. But this is what I want. I should launch the app and I should see MainActivity. If I click SecondActivity, I should see a ListView. If I clicked a ListView item, I should go to ThirdActivity with my clicked item results.

I know empty doesn't mean null and it's normal that the app thinks even if it's empty I have results. I searched a lot of topics about if/else conditions on StackOverflow and tried a lot of solutions.

If my condition is true, my app launches ThirdActivity directly with empty results. When I go back to MainActivity, click SecondActivity, and click some item, I can go to ThirdActivity with right results. If my condition is false, my app launches MainActivity but even if I click a ListView item I can't get the results.

Here is what I have tried until now

KEY_NAME != null

KEY_NAME == null

KEY_NAME.isEmpty()

!KEY_NAME.isEmpty()

KEY_NAME.equals("")

in SecondActivity or MainActivity

final String KEY_NAME = null;

final String KEY_NAME = "";

and many more.

Everything I tried, I tried again with just the if condition like this

if (KEY_NAME.equals("")){

            final Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    intent.putExtra(KEY_NAME, name);
                    startActivityForResult(intent, MANTRA_DETAILS_ACTIVITY_REQUEST_CODE);
                }
            });
        }

I hope I could explain myself truly. Can you help me please?

Aucun commentaire:

Enregistrer un commentaire