What I am aiming to do here is to change the picture of "ibChamp" from the default one to "ahri". Note that the names inside the ***s are the names of the activity.
***CreateBuilds.java***
ibChamp.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent champs = new Intent(CreateBuilds.this, Champions.class);
//creates the Intent champs for startActivityForResult()
startActivityForResult(champs, 0);
//opens up Champions.class layout
}
});
protected void onActivityResult(int requestCode, int resultCode, int data){
//starts when this.finish() from Champions is ran
ImageButton ibChamp = (ImageButton) findViewById(R.id.ibChamp);
//creates the ImageButton ibChamp
ibChamp.setImageResource(R.drawable.ahri);
//sets the picture of ibChamp to "ahri"
};
***Champions.java****
private myapplicationtest mytestInst = new myapplicationtest();
public void changePicture(final int champion){
mytestInst.setInt(champion);
//runs "setInt" from the myapplicationtest class
this.finish();
//closes this current layout to run onActivityResult
}
In this code, the onActivityResult() does not seem to run, since after "Champions.java" is finished, "ibChamp"'s picture did not change. If there is something extremely obvious, please state it, and any questions are welcomed.
finish() will terminate the Activity. IMHO, it does not make sense to call finish() and then do layout changes (I ssume on the view in the Activity to be finished).
Change "int data" into "Intent data":
protected void onActivityResult(int requestCode, int resultCode, int data)
---->
protected void onActivityResult(int requestCode, int resultCode, Intent data)
Related
This is my First Activity where i want to change button color by pressing button on another activity
public void colorchangeOnfirstActivity(){
Button btnA = (Button) findViewById(R.id.asmat_btn);
btnA.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
}
This is my second Activity where the second activity button is.
Button btnB = (Button) findViewById(R.id.rose_btn);
btnB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
asmat_activity secondActivity = new asmat_activity();
asmat_activity.colorchangeOnfirstActivity();
}
});
Activities should never directly interact with each other. Generally, if you want to do something in response to an action in another Activity, you should use the Activity Result APIs.
So, for example, you may start your secondary Activity with a request code:
// The request code can be any integer value you wish
startActivityForResult(activityIntent, MY_REQUEST_CODE);
Then in your new Activity, you can set a result to be delivered back to the Activity which started it. In your case, something like:
btnB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// This will deliver the result to the requesting Activity
setResult(RESULT_OK);
finish();
}
}
Then in your first Activity, you will override onActivityResult to handle the result:
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (requestCode == MY_REQUEST_CODE) {
btnA.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
I have a problem with updates my recyclerView. When I get unswer (it`s ArrayList with objects) in method onActivityResult I call the custom update method, where I update my ArrayList, but nothing happens...
I try to do it like in this topic how to update RecyclerView element from OnActivityResult
It`s my code :
Parent`s activity :
ArrayList<Unswer> unswerFromMain;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (data==null){
Log.d("LOGO", "no unswer" );
return;
} else {
unswerFromMain = (ArrayList<Unswer>) data.getSerializableExtra("aboutGROUPS");
recyclerAdapter.updateAdapter(unswerFromMain);
}
}
}
RecyclerAdapter class :
private ArrayList<Unswer> getUnswer = new ArrayList<>();
public void updateAdapter (ArrayList<Unswer> updateUnswer){
getUnswer = updateUnswer;
notifyDataSetChanged();
}
Try
getUnswer.clear()
getUnswer.addAll(updateUnswer);
instead of
getUnswer = updateUnswer;
Hello fellow programmers,
I'm experiencing some issues regarding my Android App, i'm currently
working on. For this purpose, I only need to mention that I have two
Activities (One is called MainActivity.class and the second is called
FilterActivity.class).
The purpose of my MainActiviy class is to display a movie (Genres,
year, rating etc) + a trailer of the specifik video.
In the OnCreate method for MainActiviy, im initializing the
YouTubePlayerView (since I want a random movie to pop up as soon as
you open the application).
The purpose of my FilterActivity class is to choose some specfik
search criterias for a movie.
I'm opening FilterActivity from MainActivity like this:
public void openFilter(){
Intent askIntent = new Intent(this, FilterActivity.class);
startActivityForResult(askIntent, 1); }
And in my FilterActivity im sending the information from a newly
created movie like this:
movieIntent.putExtra("url", a.getUrl());
movieIntent.putExtra("title", a.getTitle());
movieIntent.putExtra("rating", (String.valueOf(a.getRating())));
movieIntent.putExtra("plot", a.getDesc());
movieIntent.putExtra("year", (String.valueOf(a.getYear())));
movieIntent.putExtra("genre", a.getGenres());
setResult(RESULT_OK, movieIntent);
finish();
And this is how I fetch data from MainActivity:
protected void onActivityResult(int requestCode, int resultCode, final Intent data){
if(resultCode == RESULT_OK){
titleView.setText(data.getStringExtra("title"));
ratingView.setText(data.getStringExtra("rating"));
plotView.setText(data.getStringExtra("plot"));
yearView.setText(data.getStringExtra("year"));
genreView.setText(data.getStringExtra("genre"));
url = data.getStringExtra("url"); }
This is basically what I need to show. (This is all works by the way):
I'm getting a newly created movie and the criterias match.
However, in the OnActivityResult, I can't get my YoutubePlayerView to
re-load the video with the specific URL. The old video is still there,
and playable. I have checked and I am indeed getting a new URL from
the FilterActivity.
The only way I'm coming around this issue is by basically reloading
the activity, and then (since im creating a random movie in my
OnCreate method), the criteria don't match.
Any suggestions would be appreciated!
Sincerely
In onActivityResult, release current player and re-initialize YoutubePlayerView :
#Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
if (resultCode == RESULT_OK) {
mVideoId = getVideoId(data.getStringExtra("url"));
mPlayer.release();
mYoutubeplayerView.initialize(mApiKey, this);
}
}
A complete example of MainActivity is :
public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
String mVideoId = "5xVh-7ywKpE";
String mApiKey = "YOUR_API_KEY";
YouTubePlayerView mYoutubeplayerView;
YouTubePlayer mPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mYoutubeplayerView = (YouTubePlayerView) findViewById(R.id.player);
mYoutubeplayerView.initialize(mApiKey, this);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openFilter();
}
});
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer youTubePlayer, boolean b) {
mPlayer = youTubePlayer;
mPlayer.loadVideo(mVideoId);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
}
#Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
if (resultCode == RESULT_OK) {
mVideoId = getVideoId(data.getStringExtra("url"));
mPlayer.release();
mYoutubeplayerView.initialize(mApiKey, this);
}
}
private String getVideoId(String url) {
String pattern = "(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(url);
if (matcher.find()) {
return matcher.group();
}
return "";
}
public void openFilter() {
Intent askIntent = new Intent(this, FilterActivity.class);
startActivityForResult(askIntent, 1);
}
}
Note that I've used this post to extract Youtube videoId from url path
Further to my previous post, I now want to invoke a child activity from the main activity a number of times. In my real project (as opposed to the noddy test below), when the child activity is invoked, its header displays, "Enter first data set" then invites the user to enter some data. This data is actually stored in a common class rather than being returned to the main activity. Then the child needs to be called again with a new prompt "Enter second data set", and the same thing happens.
What I cannot work out is how to do this. If I include two calls to the child, every time, only the second call appears to happen, the prompt appearing in the child activity being "Enter second data set" every time. This startActivityForResult() method is I believe, designed to be used when you want to call an activity and wait for the result (which you do with an onActivityResult() do you not), but it does not wait.
How on earth do I do this? Sample code follows.
Thank you to anyone who can clearly explain where I'm going wrong and what the right code should be.
MainActivity code extract
#Override
public void onResume(){
super.onResume();
TextView maintop = (TextView)findViewById(R.id.maintop);
maintop.setText(Common.mess1);
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainbutton = (Button)findViewById(R.id.mainbutton);
mainbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent1 = new Intent(MainActivity.this,Child.class);
intent1.putExtra("Prompt", "Enter first data set");
startActivityForResult(intent1,1);
onActivityResult(1,1,intent1);
}
});
mainbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent2 = new Intent(MainActivity.this,Child.class);
intent2.putExtra("Prompt", "Enter second data set");
startActivityForResult(intent2,1);
onActivityResult(1,1,intent2);
}
});
}
You can only have one click listener in the button, so when you call set for the 2nd time it replaces the listener.
What you need to do is set the click listener for the enter first data, don't call to onActivityResult(1,1,intent1) that's not how you do it, you need override the method, and in onActivityResult call the 2nd.
Something like this:
static final int FIRST_INTENT = 1;
static final int SECOND_INTENT = 2;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainbutton = (Button)findViewById(R.id.mainbutton);
mainbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent1 = new Intent(MainActivity.this,Child.class);
intent1.putExtra("Prompt", "Enter first data set");
startActivityForResult(intent1,FIRST_INTENT);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FIRST_INTENT) {
if (resultCode == RESULT_OK) {
Intent intent2 = new Intent(MainActivity.this,Child.class);
intent2.putExtra("Prompt", "Enter second data set");
startActivityForResult(intent2,SECOND_INTENT);
}
}
}
And in your child activity
//DO SOMETHING
....
setResult(RESULT_OK)
finish();
}
For more check
[http://developer.android.com/intl/es/training/basics/intents/result.html]
[http://developer.android.com/intl/es/reference/android/app/Activity.html#setResult%28int%29]
onActivityResult() is a standard Android function that is called after a child Activity closes. However, it doesn't seem to close all the way.
After my child activity finishes, onActivityResult() is called in the parent. At this point, my action is to inject a context (through a provider, non-assisted) in a new class the parent is creating, using the parcelable information that the child has just given back to me for an #Assisted parameter in that new class.
However, despite finish() being called on the child, the context that is injected is not the parent--it is the child! This kills the program.
How do I get around this?
Here's some code that gives you an idea of what I'm doing.
In the parent:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_NEW_EXERCISE)
{
if (resultCode == RESULT_OK)
{
EntityExercise exercise = (EntityExercise)data.getExtras().get("exercise");
addNewRoutineExerciseDetail(exercise);
//Toast.makeText(this, exercise.getName(), Toast.LENGTH_LONG).show();
}
}
}
public RoutineExerciseDetail addNewRoutineExerciseDetail(EntityExercise exercise)
{
RoutineExerciseDetail detail = detailFactory.create(exercise);
detail.setOnClickRelativeLayoutListener(mEditParamsOnClickListener);
return detail;
}
In the child:
View.OnClickListener mListenerReturnExercise = new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent resultIntent = new Intent();
resultIntent.putExtra("exercise", (EntityExercise)v.getTag()); //Assuming it's the tag
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
};
RoutineExerciseDetail's constructor's parameters:
#Inject
public RoutineExerciseDetail(ActivityBaseRoboOrm<DatabaseHelper> context, List<RoutineExerciseDetail> list,
#AddEditExercise TableLayout layout, #Assisted EntityExercise exercise)
Yes, this will fail on RoboGuice 1.1. Activity.onActivityResult() is a somewhat unusual method in that it executes before the activity's onResume() is called, thus RoboGuice doesn't know to switch the context back to the caller activity.
One of the main changes in RoboGuice 1.2 is to fix this behavior. If you switch to 1.2 and replace any providers with ContextScopedProviders per these instructions, you should be good to go.
If you need to stay with RoboGuice 1.1, you should be able to scope your context manually the following way:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
scope.enter(this);
try {
...
} finally {
scope.exit(this);
}
}
In ActivityForResult method in Android your requestcode should be same in both the Activity.then and only then your code will work. I hope it will help you.