-2

The problem i've stuck is I have 4 activities namely A, B ,C and D . the flow is first A then B then C and so on. I want to pass data of A to B and then data A+B to C , then data of A+B+C to D and so on.. I've used in Activity A Intent of Hashmap

HashMap<String, String> hashMap = new HashMap<>(); //declared Globally
hashMap.put("key", value);
i.putExtra("map", hashMap); (i is the Intent Object)
startActivity(i); //Starting the Intent

And on receiving side i.e Activity B

HashMap<String, String> hashMap = (HashMap<String, String>)i.getSerializableExtra("map");

Here i'm able to get Successfully the data , but when i try to pass on foward this data to next activity i get null values causing NullPointerException. In the B activity

Hashmap<String,String> hashMap2 = new HashMap<>;
hashMap2.put("key",hashMap.get("key"));
Log.i("Value:",hashMap.get("key"));

Here i get the values successfully put when same way i pass hashmap2 to C activity i get NullPointerException. Not understanding what's wrong in here.

I want to pass the values and not store them so i'm preferring Intents over Shared Preferences.

Thanks for your help .I've found out why it was giving me null values 1)In B activity I was doing the wrong way to get the values i.e first getIntent and then sum of A+B values i.e putExtra should be used only when I declare the new intent for the C class. As i was first doing putExtra and then new Intent to C , so in C i used to get Null Values.

4 Answers4

1

This might help you.You can simply send values like this and it will work.

In Activity A:

Intent intent=new Intent(ActivityA.this,ActivityB.class);
intent.putExtra("key1","value1");
startActivity(intent);

In Activity B:

String value1=getIntent.getStringExtra("key1");
Intent intent=new Intent(ActivityB.this,ActivityC.class);
intent.putExtra("key1",value1);
intent.putExtra("key2","value2");
startActivity(intent);

In Activity C:

  String value1=getIntent.getStringExtra("key1");
  String value2=getIntent.getStringExtra("key2");
  Intent intent=new Intent(ActivityC.this,ActivityD.class);
  intent.putExtra("key1",value1);
  intent.putExtra("key2",value2);
  intent.putExtra("key3","value3");
  startActivity(intent);

In Activity D,

String value1=getIntent.getStringExtra("key1");
String value2=getIntent.getStringExtra("key2");
String value3=getIntent.getStringExtra("key3");
Android Geek
  • 8,956
  • 2
  • 21
  • 35
1

It is really hectic to send value from one activity to another using Intent same code written in every activity but alternative way is to use cache

 public class CacheManager {
        public static Map<String, Object> cachedData = new HashMap<>();


        public static void clearCache() {
            cachedData.clear();

        }

        public static void putIntoCache(String key, Object value) {
            CacheManager.cachedData.put(key, value);
        }

        public static Object getFromCache(String key) {
            if (cachedData.containsKey(key)) {
                return CacheManager.cachedData.get(key);
            } else {
                return null;
            }

        }
    }

One of the advantage of this approch is you can modify data where ever you want and put modified data in cache again

step 1- Create class called cacheManager

Step 2- from anywhere you can put value into Cache HashMap

step 3- from anywhere you can get Value From Cache

Enjoy !

Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
  • Is using of a static cache really the right approach? - there are other approaches which are more object oriented – wake-0 Feb 09 '17 at 06:35
  • yes it is !! in these approch you can access data anywhere in application and also allow you to modify where ever you want !! its easy to impliment and understand and less confusing ............................just think on it – Sushant Gosavi Feb 09 '17 at 06:38
  • what do you mean by object oriented this is also object oriented .......i am not able to understand what you want to say – Sushant Gosavi Feb 09 '17 at 06:39
  • instead i would use a DI Framework like Dagger2 and create a Singleton ... but yeah who knows :) its a matter of style – wake-0 Feb 09 '17 at 06:40
  • as per your requirement you can change that !! have a great day – Sushant Gosavi Feb 09 '17 at 06:44
  • This is a convenient way to share members, but it is *not* a cache. It is a map that pins memory for the (executing) lifetime of your app. It will only ever grow and never evicts stale values. – Krylez Feb 09 '17 at 21:05
0

use intent.putExtra in intent like this.

   Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
   String strName = null;
   i.putExtra("STRING_I_NEED", strName);
   startActivity(i);/

and retrieve .

     String newString;
     if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
    newString= null;
} else {
    newString= extras.getString("STRING_I_NEED");
}
       } else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
android_jain
  • 788
  • 8
  • 19
0

First Option

Store data in application context

Declare a class that should extend Application

public class HashTableBundle extends Application {

    private Hashtable<String,String> mHashTable  = null;

    public Hashtable<String, String> getmHashTable() {
        return mHashTable;
    }

    public void setmHashTable(Hashtable<String, String> mHashTable) {
        this.mHashTable = mHashTable;
    }
}

You can set data into application context by

HashTableBundle mStat = (HashTableBundle) getApplicationContext();
mStat.setmHashTable(mMainHashTable);

You can get it from any activity by using

HashTableBundle mStat = (HashTableBundle) getApplicationContext();
 Hashtable<String,String> mHashTable = mStat. getmHashTable();

You need to add HashTableBundle in AndroidManifest otherwise application Throws class cast exception

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name=".HashTableBundle"> . . . </application>

Second option

MainActivity.java



public class MainActivity extends AppCompatActivity {
    Hashtable<String,String> mMainHashTable = new Hashtable<>();
     MHashBundle mm = new MHashBundle();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMainHashTable.put("hai","I am From MainActivity");
        mm.setmHashTable(mMainHashTable);
        Intent mIntent = new Intent(MainActivity.this,ClassB.class);
        mIntent.putExtra("myhashtable", mm);
        System.out.println("data Main Activity :"+mMainHashTable.get("hai"));
        startActivity(mIntent);
        this.finish();


    }
}

Hashtable container MHashBundle.java

public class MHashBundle implements Serializable {
    private Hashtable<String,String> mHashTable  = null;

    public Hashtable<String, String> getmHashTable() {
        return mHashTable;
    }

    public void setmHashTable(Hashtable<String, String> mHashTable) {
        this.mHashTable = mHashTable;
    }
}

note MHashBundle.java must implement Serializable

Second Activity ClassB.java

public class ClassB extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent mIntent = new Intent(ClassB.this,ClassC.class);
        MHashBundle mBundle = (MHashBundle) getIntent().getSerializableExtra("myhashtable");
        mIntent.putExtra("myhashtable",mBundle);
        System.out.println("data class B :"+mBundle.getmHashTable().get("hai"));
        startActivity(mIntent);
        this.finish();
    }

Thrird Activity

public class ClassC extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            MHashBundle mBundle = (MHashBundle) getIntent().getSerializableExtra("myhashtable");

        System.out.println("data class C :"+mBundle.getmHashTable().get("hai"));

    }
}
CLIFFORD P Y
  • 16,974
  • 6
  • 30
  • 45