Reputation: 3
Hi I need to get a String from an Activity to another Class. I need the String VideoID in the Test2 class. I wanted to use sharedpreferences, but this does not work. How can i use sharedpreferences here? Or any other idea how i can get the String? Thanks
In my Activity I have this Button:
// Video Button
final Button videobutton = (Button) findViewById(R.id.videobutton);
videobutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Video ID save
SharedPreferences.Editor editor = getSharedPreferences("VideoID_saver", MODE_PRIVATE).edit();
editor.putString("VideoID", current_video);
editor.commit();
Intent OpenTest = new Intent(Questions.this, Test2.class);
startActivity(OpenTest);
}
});
The Test2.class looks like this:
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.ErrorReason;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
public class Test2 extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "AIzaSyCb2pCHbqhBDKaYzFvU7g1SBG14YrM3XWE";
//Test
SharedPreferences prefs = getSharedPreferences("VideoID_saver", MODE_PRIVATE);
String testtext = prefs.getString("VideoID", "");
//http://youtu.be/<VIDEO_ID>
public final String VIDEO_ID = testtext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
/** Initializing YouTube player view **/
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(API_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
@Override
public void onBuffering(boolean arg0) {}
@Override
public void onPaused() {}
@Override
public void onPlaying() {}
@Override
public void onSeekTo(int arg0) {}
@Override
public void onStopped() {}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
@Override
public void onAdStarted() {}
@Override
public void onError(ErrorReason arg0) {}
@Override
public void onLoaded(String arg0) {}
@Override
public void onLoading() {}
@Override
public void onVideoEnded() {}
@Override
public void onVideoStarted() {}
};
Upvotes: 0
Views: 909
Reputation: 7683
You're probably somehow accessing different SharedPreferences instances. It's easier to make sure this doesn't happen using the getDefaultSharedPreferences()
method:
In you Activity:
PreferenceManager.getDefaultSharedPreferences(this)
.edit()
.putString("VideoID", current_video)
.apply();
In your Test2 onCreate
method:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String testtext = prefs.getString("VideoID", "");
Upvotes: 1
Reputation: 3593
Your goal is to get a string from button to test2.
It seems like it would be better to avoid shared preferences entirely and pass the string to the next activity directly via the put/getExtra function.
Like so:
Intent OpenTest = new Intent(Questions.this, Test2.class);
i.putExtra("VideoID", current_video);
startActivity(OpenTest);
Then extract it from your new activity in your OnCreate
method:
//http://youtu.be/<VIDEO_ID>
public final String VIDEO_ID;
@
Override
protected void onCreate(Bundle savedInstanceState) {
//Test
Intent intent = getIntent();
VIDEO_ID = intent.getExtras().getString("VideoID");
}
Upvotes: 0
Reputation: 81
Couple of things. Test2 is inheriting an activity it seems, so not sure why your title says Non-Activity. SharedPreferences reading and writing seems ok but maybe reading the sharedpreferences even before the constructor on the activity has had change to run is too early.
Upvotes: 0