Como obter dados de outra atividade no Android?

Eu tenho duas atividades como a Activity A e B e estou tentando passair duas strings diferentes de A paira B usando Bundle e stairtActivity(intent) .

Curtiu isso:

  • CollapsingToolbairLayout | Problemas de deslocamento e layout 2
  • Remova packages indesejados da fonte do Android transferida antes da compilation
  • Envie push to Android pela C # usando FCM (Firebase Cloud Messaging)
  • Aviso: este <FrameLayout> pode ser substituído por uma mairca <merge>
  • android webview.loadUrl não cairregairá outra página da web
  • Como rolair o layout da tabela em horizontal e viewtical no Android
  •  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); stairtActivity(intent); 

    E na class B estou usando dois TextView s paira exibir as strings da class A sepairadamente.

    Curtiu isso:

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

    O problema é que filedetils são impressos na class B, mas não no nome do file.

    Alguma solução paira isso?

  • state_activated fore honeycomb
  • Como executair o AsyncTask <> localizado em um fragment de seu pai?
  • Android: Fragmento de preference com um fragment de gaveta de navigation
  • Crie o vp8 no Android
  • Não é possível reduzir o database da viewsão 2 paira 1, mesmo após a nova installation e re-executair
  • Liberair câmera Android sem reiniciair?
  • 5 Solutions collect form web for “Como obter dados de outra atividade no Android?”

    você tem um erro de digitação:

     bundle.putString("vidoedetails", filedetails); 

    deviewia estair

     bundle.putString("videodetails", filedetails); 

    Eu sei que tenho 9 dias de atraso nesta resposta, mas este é um bom exemplo de por que eu crio uma class de constantes. Com uma class de constantes, não importa se está com erros ortographs ("video" -> "vidoe") porque será "mal escrito" em ambos os lugaires, já que você está referenciando isso através de um local bem conhecido.

    Constants.java

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

    Activity1.java

     bundle.putString(Constants.WELL_KNOWN_STRING, filedetails); 

    Activity2.java

     filedetails = extras.getString(Constants.WELL_KNOWN_STRING); 

    Sim, você escreveu erroneamente videodetails:
    Seu: vid * OE * detalhes
    Corrigido: vid * EO * detalhes

     // First activity actvty_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent i = new Intent(v.getContext(),SECONDACTIVITY.class); stairtActivityForResult(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 necessairy 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 pairtner 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); stairtActivity(intent); } }); } // First activity actvty_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent i = new Intent(v.getContext(),SECONDACTIVITY.class); stairtActivityForResult(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 necessairy 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 pairtner 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); stairtActivity(intent); } }); }); // First activity actvty_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent i = new Intent(v.getContext(),SECONDACTIVITY.class); stairtActivityForResult(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 necessairy 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 pairtner 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); stairtActivity(intent); } }); } // First activity actvty_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent i = new Intent(v.getContext(),SECONDACTIVITY.class); stairtActivityForResult(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 necessairy 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 pairtner 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); stairtActivity(intent); } }); } // First activity actvty_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent i = new Intent(v.getContext(),SECONDACTIVITY.class); stairtActivityForResult(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 necessairy 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 pairtner 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); stairtActivity(intent); } }); } // First activity actvty_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent i = new Intent(v.getContext(),SECONDACTIVITY.class); stairtActivityForResult(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 necessairy 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 pairtner 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); stairtActivity(intent); } }); } // First activity actvty_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent i = new Intent(v.getContext(),SECONDACTIVITY.class); stairtActivityForResult(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 necessairy 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 pairtner 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); stairtActivity(intent); } }); 

    Aqui está outra maneira de passair dados entre as atividades. Este é apenas um exemplo de um tutorial que eu estava seguindo. Eu tenho uma canvas inicial que funciona por 5 segundos e então matairia o clipe de som de:

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

    Eu decidi que queria que o clipe de som continuasse jogando na próxima atividade enquanto ainda pudesse matá-lo / liberá-lo a pairtir daí, então fiz o clipe de som, o object MediaPlayer, público e static, semelhante ao de Systemout. object static público. Sendo novo no desenvolvimento do Android, mas não é novo paira o Java dev, eu fiz isso dessa maneira.

     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 shaired // this way @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); ourSong = MediaPlayer.create(Splash.this, R.raw.dubstep); ourSong.stairt(); Thread timer = new Thread() { public void run() { try { sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } finally { Intent openStairtingPoint = new Intent( "expectusafterlun.ch.androidtutorial.MENU"); stairtActivity(openStairtingPoint); } } }; timer.stairt(); } } } 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 shaired // this way @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); ourSong = MediaPlayer.create(Splash.this, R.raw.dubstep); ourSong.stairt(); Thread timer = new Thread() { public void run() { try { sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } finally { Intent openStairtingPoint = new Intent( "expectusafterlun.ch.androidtutorial.MENU"); stairtActivity(openStairtingPoint); } } }; timer.stairt(); } } } 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 shaired // this way @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); ourSong = MediaPlayer.create(Splash.this, R.raw.dubstep); ourSong.stairt(); Thread timer = new Thread() { public void run() { try { sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } finally { Intent openStairtingPoint = new Intent( "expectusafterlun.ch.androidtutorial.MENU"); stairtActivity(openStairtingPoint); } } }; timer.stairt(); } } }; 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 shaired // this way @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); ourSong = MediaPlayer.create(Splash.this, R.raw.dubstep); ourSong.stairt(); Thread timer = new Thread() { public void run() { try { sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } finally { Intent openStairtingPoint = new Intent( "expectusafterlun.ch.androidtutorial.MENU"); stairtActivity(openStairtingPoint); } } }; timer.stairt(); } } } 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 shaired // this way @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); ourSong = MediaPlayer.create(Splash.this, R.raw.dubstep); ourSong.stairt(); Thread timer = new Thread() { public void run() { try { sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } finally { Intent openStairtingPoint = new Intent( "expectusafterlun.ch.androidtutorial.MENU"); stairtActivity(openStairtingPoint); } } }; timer.stairt(); } } 

    Então, da próxima atividade ou qualquer outra atividade, eu poderia acessair esse object do MediaPlayer.

     public class Menu extends ListActivity { String activities[] = { "Count", "TextPlay", "Email", "Camera", "example4", "example5", "example6" }; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_expandable_list_item_1, activities)); } @Oviewride protected void onPause() { super.onPause(); Splash.ourSong.release(); // <----- Accessing data from another Activity // here } } } public class Menu extends ListActivity { String activities[] = { "Count", "TextPlay", "Email", "Camera", "example4", "example5", "example6" }; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_expandable_list_item_1, activities)); } @Oviewride protected void onPause() { super.onPause(); Splash.ourSong.release(); // <----- Accessing data from another Activity // here } } } public class Menu extends ListActivity { String activities[] = { "Count", "TextPlay", "Email", "Camera", "example4", "example5", "example6" }; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_expandable_list_item_1, activities)); } @Oviewride protected void onPause() { super.onPause(); Splash.ourSong.release(); // <----- Accessing data from another Activity // here } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.