How to get data from another activity in android?

I have two actions, such as Activity A and B, and I'm trying to pass two different lines from A to B using Bundle and startActivity(intent) .

Like:

 Intent intent = new Intent(A.this, B.class); Bundle bundle = new Bundle(); bundle.putString("vidoedetails", filedetails); //bundle.putString("videoname", filename); intent.putExtras(bundle); //intent.putExtra("videofilename", filename); //intent.putExtra("vidoefiledetails", filedetails); startActivity(intent); 

And in class B, I use two TextView to display rows from class A separately.

Like:

 Intent i = getIntent(); Bundle extras = i.getExtras(); filedetails = extras.getString("videodetails"); filename = extras.getString("videoname"); 

The problem of filedetils is printed in class B, but not in the file name.

Any solution for this?

+7
source share
5 answers

you have a typo:

 bundle.putString("vidoedetails", filedetails); 

it should be

 bundle.putString("videodetails", filedetails); 
+19
source

I know that I'm 9 days late for this answer, but this is a good example of why I create a class of constants. With a class of constants, it doesn’t matter if it is written with an error ("video" β†’ "vidoe"), because it will be "with an error" in both places, because you refer to it through a well-known location.

Constants.java

 public static String WELL_KNOWN_STRING "org.example.stackoverflow.4792829"; 

Activity1.java

 bundle.putString(Constants.WELL_KNOWN_STRING, filedetails); 

Activity2.java

 filedetails = extras.getString(Constants.WELL_KNOWN_STRING); 
+9
source

Yes, you incorrectly indicated the video:
Yours: vid * OE * more
Right: vid * EO * details

+2
source
 // First activity actvty_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent i = new Intent(v.getContext(),SECONDACTIVITY.class); startActivityForResult(i, STATIC_INTEGER_VALUE); } }); /* This function gets the value from the other activity where we have passed a value on calling this activity */ public void activity_value() { Intent i = getIntent(); Bundle extras=i.getExtras(); if(extras !=null) { // This is necessary for the retrv_value rtrv_value = extras.getString("key"); if(!(rtrv_value.isEmpty())) { // It displays if the retrieved value is not equal to zero myselection.setText("Your partner says = " + rtrv_value); } } } // Second activity myBtn.setOnClickListener(new View.OnClickListener () { public void onClick(View v) { Intent intent = new Intent(v.getContext(), FIRSTACTIVITY.class); Bundle bundle = new Bundle(); bundle.putString("key", txt1.getText().toString()); // Here key is just the "Reference Name" and txt1 is the EditText value intent.putExtras(bundle); startActivity(intent); } }); 
0
source

Here is another way to transfer data between actions. This is just an example from the tutorial that I followed. I have a splash screen that works for 5 seconds and then it will remove the sound clip from:

 @Override protected void onPause() { super.onPause(); ourSong.release(); } 

I decided that I want the sound clip to continue playing the next activity, while still being able to kill / release it from there, so I made a sound clip, a MediaPlayer object, open and static, similar to what it was in System. out is a public static object. Being new to Android dev, but not new to Java dev, I did it this way.

 import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; public class Splash extends Activity { public static MediaPlayer ourSong; // <----- Created the object to be shared // this way @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); ourSong = MediaPlayer.create(Splash.this, R.raw.dubstep); ourSong.start(); Thread timer = new Thread() { public void run() { try { sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } finally { Intent openStartingPoint = new Intent( "expectusafterlun.ch.androidtutorial.MENU"); startActivity(openStartingPoint); } } }; timer.start(); } } 

Then, from the next action or any other action, I could access this MediaPlayer object.

 public class Menu extends ListActivity { String activities[] = { "Count", "TextPlay", "Email", "Camera", "example4", "example5", "example6" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_expandable_list_item_1, activities)); } @Override protected void onPause() { super.onPause(); Splash.ourSong.release(); // <----- Accessing data from another Activity // here } } 
0
source

All Articles