Skip to content Skip to sidebar Skip to footer

How To Send Data Through Intent In Android Without Opening Another Activity?

here is my code for sending data by intent but i don't want open another activity i just want to send the data without opening it.. Bundle contain = new Bundle(); conta

Solution 1:

Yes i have also faced this problem .

Many developers also facing problems when passing data from dialog to another activity through Intent or Bundle. It returns null at the retrieving time from another activity.

And the only solution is SharedPreferences.

But you have to place it inside the dismiss button.( ex: ok/cancel etc)

And retrieve the data from another activity easily through the same key. Do not use any service followed by broadcast intent .

The code in dialog activity is like this:

    AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
    builder.setIcon(R.drawable.mailicon);
    builder.setTitle(name);
    builder.setView(view);

    builder.setPositiveButton("Send Request",newDialogInterface.OnClickListener()
    {

     @OverridepublicvoidonClick(DialogInterface dialog,    int which) {
     StringmailID= id.getText().toString();

     //Here define all your sharedpreferences code with key and valueSharedPreferencesprefs= getSharedPreferences("my_prefs", MODE_PRIVATE);
     SharedPreferences.Editoredit= prefs.edit();
     edit.putString("MID", mailID );
     edit.commit();

    }

});

And from another fetch the data like this:

SharedPreferencesbb= getSharedPreferences("my_prefs", 0);
    Stringm= bb.getString("NUM", "");
    Toast.makeText(this, m, Toast.LENGTH_SHORT).show();

Add some checkings for a good standard.

Thank you

Solution 2:

You call also use SharedPreferences to archieve that

Solution 3:

You probably want to use a Service not an activity.

Read: http://developer.android.com/guide/components/fundamentals.html#Components

Solution 4:

You can try EventBus or Otto Android libraries to communicate between activities, services and fragments..

So you should create a Service to pass data and for communication between activities, fragments etc use an event bus

Post a Comment for "How To Send Data Through Intent In Android Without Opening Another Activity?"