开发者

How to get data from other activity in android?

开发者 https://www.devze.com 2023-02-06 20:27 出处:网络
I have two activities such as Activity A and B and I\'m trying to pass two different strings from A to B using Bundle and startActivity(intent).

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

Like that:

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'm using two TextViews to display the strings from class A seperately.

Like that:

Intent i = getIntent();
Bundle extras = i.getExtras();

filedetails = extras.getString("videodetails");
filename = extras.getString("videoname")开发者_开发百科;

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

Any solution for this?


you have a typo:

bundle.putString("vidoedetails", filedetails);

should be

bundle.putString("videodetails", filedetails);


I know I am 9 days late on this answer, but this is a good example of why I create a constants class. With a constants class, it doesnt matter if it is misspelled ("video" -> "vidoe") because it will be 'misspelled' in both places as you are referencing 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);        


Yes, you spelled wrongly videodetails:
Yours: vid*OE*details
Correct: vid*EO*details


// 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);
    }
});


Here's another way to pass data between Activities. This is just an example from a tutorial I was following. I have a splash screen that runs for 5 seconds and then it would kill the sound clip from:

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

I decided I wanted the sound clip to continue playing into the next activity while still being able to kill/release it from there, so I made the sound clip, MediaPlayer object, public and static, similar to how out 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 activity, or any other activity, I could access that 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

精彩评论

暂无评论...
验证码 换一张
取 消