2

When i press button in second Activity, intent sends a string to the MainActivity. It's in onClick method:

        Intent intent = new Intent(this, MainActivity.class);
        String wynik = "clear";
        intent.putExtra("clearList", wynik);

and main activity, in onCreate method:

 Intent intent2 = getIntent();
    wynik = intent2.getStringExtra("clearList");
    if (wynik.equals("clear")) {

        tempLukasz = 0;
        tempMarcelina = 0;
        tempKarolina = 0;
        foodCategorySum = 0;
        catCategorySum = 0;
        othersCategorySum = 0;
        lukaszJedzenie = 0;
        lukaszKot = 0;
        lukaszInne = 0;
        marcelinaJedzenie = 0;
        marcelinaKot = 0;
        marcelinaInne = 0;
        karolinaJedzenie = 0;
        karolinaKot = 0;
        karolinaInne = 0;
        newPayments.clear();
        wynik = "";
    }

so when MainActivity receives this "clear" message it should set my numbers to 0 and clear the list. Pic.1 is example of my main activity window: enter image description here enter image description here

and in second pic is what i want to have AFTER button in second activity is pressed

Unfortunately it does not work: this line throws NullPointerException:

 if (wynik.equals("clear")) {

Any ideas where am I making a mistake ? I thought that checking if ( getIntent() != null ) will solve this or just surrount my IF clase with try-catch but it does not work either.

Łukasz Stasiak
  • 107
  • 1
  • 2
  • 11
  • post the exact code you use to send, and receive the intent, please – Mateus Brandao Oct 01 '15 at 17:17
  • Why do you need to call the MainActivity from the second activity, as pressing the back button would still take you back to the MainActivity. – zulkarnain shah Oct 01 '15 at 17:20
  • it's not back button, it's one of the buttons in the second activity that stores some data in a ListView in second activity and after it shows the data I want to 'reset' the first activity so that every number and list are zero'ed – Łukasz Stasiak Oct 01 '15 at 17:25
  • 2
    Try exploring startActivityForResult instead of starting MainActivity again in SecondActivity. Also, when you say you get NPE you should post the exception trace as well, without that it's not possible to help. – Sharjeel Oct 01 '15 at 17:27
  • You probably want to be using [startActivityForResult](http://developer.android.com/reference/android/app/Activity.html#StartingActivities) as Sharj mentioned. [Here is an answer I wrote up with an example](http://stackoverflow.com/questions/18243515/android-going-back-to-previous-activity-with-different-intent-value/18243541#18243541) – codeMagic Oct 01 '15 at 17:32

5 Answers5

0

The problem is that when first time the onCreate() gets called it's checking for the String extra that should have been filled by the Second Activity which hasn't been called yet. So you'd definitely get a NPE when launching the app. If you need to start another activity and receive a result back you should call startActivityForResult() instead of startActivity().

When the user is done with the second activity and returns, the system calls your activity's onActivityResult() method. This method includes three arguments: 1. The request code you passed to startActivityForResult(). 2.A result code specified by the second activity. This is either RESULT_OK if the operation was successful or RESULT_CANCELED if the user backedout or the operation failed for some reason. 3.An Intent that carries the result data.

Look here for full description Launching an explicit activity for getting back result

zulkarnain shah
  • 971
  • 9
  • 20
  • But but .... after I press button in my SecondaryActivity I do not want to get me straight to MainActivity. I just want make some calculations in the SecondaryActivity and send string/boolean/whatever to MainActivity AND when I press back button to get me back to Main, my clean/reset values code should be executed. I tried using code as someone here wrote: (intent2 != null && wynik != null && wynik.equals("clear")) but it does not execute the code either (values are not reset and when i tried to put a toast here it did not show up). – Łukasz Stasiak Oct 01 '15 at 18:11
  • If you just need to set some values to be used by the MainActivity then a quick fix would be to create static fields in the MainActivity and set them from the second activity so that you could update their values from any class in your project and also always get their updated values whenever needed – zulkarnain shah Oct 01 '15 at 18:16
  • sounds pretty simple, I will try this approach in couple of minutes. thanks for your help, gotta update if it works or it doesn't – Łukasz Stasiak Oct 01 '15 at 18:24
0

It's not enough checking if the intent is null, you also need to check if the String itself is null.

From developer.android.com:
Returns
the value of an item that previously added with putExtra() or null if no String value was found.

Source: http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)

So change it to:

if (intent2 != null && wynik != null && wynik.equals("clear")) {
    ... 
}
Moonbloom
  • 7,738
  • 3
  • 26
  • 38
  • I tried this code and it helped with the NPE but code is not being executed - values are not reset and when i tried to put a toast here it did not show up). – Łukasz Stasiak Oct 01 '15 at 18:12
0

The main thing that I notice odd on this attempt is the null exeception that should occur on start, another thing is intents when used to pass values backwards we tend to use onActivityResult for this.

Such when setting your intent

Intent intent = new Intent(this, MainActivity.class);
String wynik = "clear";
intent.putExtra("clearList", wynik);

we set the result to

setResult(x, intent);

and then we recieve the intent in the main class by calling onActivityResult

such as

 @Override 
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
      //dosomething with intent
 }

but also need to remember to set the intent that called the second activity to startActivityForResult(intent, x);

Ashley Alvarado
  • 1,078
  • 10
  • 17
  • im not sure if I understand it properly. So I should write `Intent intent2 = new Intent(this, MainActivity.class); String wynik = "clear"; intent2.putExtra("clearList", wynik); setResult(10, intent2);` and where should I use the onActivityResult method ? my clean-list/reset-values code should be inside the 'onActivityResult' in MainActivity but where should I call it ? – Łukasz Stasiak Oct 01 '15 at 18:14
  • Your right that the onActivityResult should be in your mainactivity and it is called automatically when an intent is started using startActivityForResult and that view is dismissed. – Ashley Alvarado Oct 01 '15 at 18:45
  • but I don't want from any place in SecondaryActivity to start MainActivity for result. The only time I want to start MainActivity is when I press back button, which happens more often than after using my CALCULATE button (which should make values on mainactivity reset) – Łukasz Stasiak Oct 01 '15 at 19:00
0

the problem is like zulkarnain said ..... try to make a static Boolean value in the second activity and name it for example : " check_for_activity" (name it any name you want) then after the put extra .. set the "check_for_activity" to true .. and in main activity ..and before the getIntent line .. type : if(secondActivity.check_for_activity == true ){ // put here the code for getting the value from the second activity }

Just try that and it maybe work :) (y)

Yaser Alosh
  • 11
  • 1
  • 2
  • I set a static boolean in second activity and in main activity i use if (boolean == true) but it does not do what I wanted. it executes the code, but I think it's not in proper place cuz even tho these valeus are set to 0, they're shown as previous numbers not 0s on screen – Łukasz Stasiak Oct 01 '15 at 18:57
0

When you are running MainActivity first time then the extra is not set by secondActivity. So its showing error. Try following

if(getIntent() != null && getIntent().getExtras() != null)
myBoolean = getIntent().getExtras().getBoolean("clearList");

Then

 if(myBoolean)

 {
  Intent intent2 = getIntent();
  wynik = intent2.getStringExtra("clearList");
if (wynik.equals("clear")) {

    tempLukasz = 0;
    tempMarcelina = 0;
    tempKarolina = 0;
    foodCategorySum = 0;
    catCategorySum = 0;
    othersCategorySum = 0;
    lukaszJedzenie = 0;
    lukaszKot = 0;
    lukaszInne = 0;
    marcelinaJedzenie = 0;
    marcelinaKot = 0;
    marcelinaInne = 0;
    karolinaJedzenie = 0;
    karolinaKot = 0;
    karolinaInne = 0;
    newPayments.clear();
    wynik = "";
}
 }
Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32