Hello.

Tell me how to transfer data between activities. I make the transition to the MainActivity and pass the data. But the data I need to get is not on the MainActivity, but on the other.

public void doSomething(Void result) { //Переход делаю на MainActivity Intent intent = new Intent(this, MainActivity.class); // Данные нужно получить на другой активности не MainActivity intent.putExtra("json", dj.getmString()); startActivity(intent); finish(); } 

Then I switch to MainActivity and from it to another activity (tooActivity). On tooActivity I try to get data:

 Intent intent = this.getIntent(); if(intent !=null) { String mString = intent.getExtras().getString("json"); System.out.print(mString); } 

But all the time an error occurs:

 TAL EXCEPTION: main Process: com.ferisov.ziam, PID: 2745 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ferisov.ziam/com.ferisov.ziam.SelliPhone}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) 

    1 answer 1

    Look for data where you sent it.

     Intent intent = this.getIntent(); if(intent !=null) { String mString = intent.getStringExtra("json", ""); System.out.print(mString); } 
    • Yes, I just thought maybe there is some way to transfer data to any activity. But the campaign is not. - Anatoly Ferisov
    • @AnatolyFerisov, there is no way out of the box. He's not needed. But you can create a basic abstract actuation and override the startActivity() method there, transfer the data from the current Intent to it and assign this data to the new Intent. So you all actviti will receive the same data with a minimum of code. But it’s still a question of whether it’s worth the cost - YuriiSPb