ActivityResultLauncher for 3 activities - java

I have 3 activities.
The first activity is a map (using Google Map API). In this activity, I also have a button that will direct to second activity.
In Second activity, it's just for additional information but there is a button. When this button is clicked, it will be directed to third activity.
In third activity, there's only a button that will bring a result to first activity (maps).
This is how I assigned my first activity's button:
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
And I also used ActivityResultLauncher in first activity like so:
ActivityResultLauncher<Intent> intentLaunch = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if(result.getResultCode() == Activity.RESULT_OK){
String data = result.getData().getStringExtra("SIMPAN");
Log.d(TAG, "berhasil");
}
}
);
In my third activity's button, I assigned it like this:
third_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("SIMPAN", "Simpan");
setResult(Activity.RESULT_OK, intent);
finish();
startActivity(new Intent(ThirdActivity.this, FirstActivity.class)); // When the button is clicked, it will get back to first activity and bringing the result
}
});
But, turns out the first activity didn't get the result. I assume that I missing this particular code after using ActivityResultLauncher:
Intent intent = new Intent(FirstActivity.this, ThirdActivity.class);
intentLaunch.launch(intent);
However, it makes the application starts from third activity instead of first activity. It does receive the result (the log was showing) but it's only one time. How can I resolve this?
(Sorry for my bad english)

This is an overkill.
Simply:
Intent intent = getIntent();
String simpan = intent.getStringExtra("SIMPAN");
in your onCreate class
registerForActivityResult is used when you get a result from outside the app, such as getting a picture from the camera.

Related

App crashes when switching from activity 1 to activity 2

In my app, there are so many activities which are referred to as levels. And one activity is Reward activity. when i win level-1, reward activity opens. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.
Houselevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
startActivity(intent);
}
if(activity.equals("level1")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
startActivity(intent);
}
}
});
}
With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. Because of this, the build will never be successful.
Also, when you declare intents, you MUST pass on the activity_name.class file.
Something you can try:
1) HouseLevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
2) HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
3) Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class);
startActivity(intent);
}
else if(activity.equals("level1")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class);
startActivity(intent);
}
}
});
}
Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually.
Here's a small article that might be able to help you with the problem
http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity
If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity.
What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity.
Your app will go back to the Activity1 or 2 whoever call it. On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK.
Then you have whatever was set on the Reward activity. The Reward activity don't need to know from where it came from if only will grab some data. So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity.
It is explain here check the accepted answer.
How to manage `startActivityForResult` on Android?

Passing Data to a new Activity on a Button Click [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 4 years ago.
I am very new to Android Studio, and have found myself stuck in this concept. I am attempting to pass Price + Name data to a Cart activity upon a button press (Add to Cart).
After attempting intent methods, it seems that after pressing "Add to Cart", the cart is opened with the data, but the data is not saved in the new activity for more additions.
Right now I have the following:
Button button = (Button) findViewById(R.id.addcart);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//How to pass information here
}
});
I am hoping to pass textView6 and textView7 to the cart activity. If possible, I would be interested in passing the image as well! Any start on this would be appreciated. Thank you!
To pass data trhoug activities you can use the same intent you use to open the new activity. You can set extras like this:
Intent i = new Intent(context, CartActivity.class);
i.putExtra("price", textView6.getText().toString());
i.putExtra("name", textView7.getText().toString());
startActivity(i);
And then in onCreate() of the just created activity you can retrieve this data getting the intent used to open this activity and getting its extras:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent data = getIntent();
String price = data.getStringExtra("price");
String name = data.getStringExtra("name");
}
Hope this help.
When you create your Intent you have to do :
Intent i = new Intent(this, ToClass.class);
i.putExtra("someName", findViewById(R.id.textView6 ).getText().toString());
i.putExtra("someName2", findViewById(R.id.textView7 ).getText().toString());
startActivity(i);
And then in your second Activity, use :
Intent intent = getIntent();
String someName= intent.getStringExtra("someName");
String someName2= intent.getStringExtra("someName2");
You say your using an intent, but what are you doing with the values? Are you saving them somewhere in the CartActivity?
For the image just pass a reference or name of the image. No need to pass the whole PNG... and again use an intent putExtra() call.

Moving of image between ImageButtons in seperate Activities onClick Bitmap

So 1 have 2 ImageButtons, clothesButton1 with an image declared in xml, and imageButton2 which is blank. Both are in seperate activities.
Upon clicking clothesButton1, i want to move the image in clothesButton1 to imageButton2 using Bitmap. clothesButton1 will become blank afterwards.
Here's my code in Java for clothesButton1:
final ImageButton clothesButton1 =(ImageButton)findViewById(R.id.clothesBtn1);
clothesButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clothesButton1.buildDrawingCache();
Bitmap bitmapclothes = clothesButton1.getDrawingCache();
Intent intent = new Intent();
intent.putExtra("BitmapClothes", bitmapclothes);
}
});
In my second activity (for imageButton2):
final ImageButton imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapClothes");
imageButton2.setImageBitmap(bitmap);
However the moving function isn't working and I really have no idea where I am wrong. Any help is greatly appreciated.
The error is because of Intent. There are two types of Intent in android Explicit and implicit intents. When you create an Intent with a Context and a Class object, you are creating an explicit intent. You use explicit intents to start activities within your application.When an activity in your application wants to start an activity in another application, you create an implicit intent. In Your case it is Explicit Intent use Intent intent=new Intent(getApplicationContext(), secondActivityName.class).
In your case
Intent intent=new Intent(getApplicationContext(),secondActivityName.class);
intent.putExtra("BitmapClothes", bitmapclothes);
startActivity(intent);
To understand more about intent read this tutorial.
I think the problem is that you didn't enabled the drawing cache on clothesButton1.
clothesButton1.setDrawingCacheEnabled(true);
Please refer:https://developer.android.com/reference/android/view/View.html#getDrawingCache%28boolean%29
I think that you can achieve this easily with Activity Transition. Take a good look at it :)
The Start an activity with a shared element section might be what you want.
First,getDrawingCache() returns null;second the way you invoke other activity is not correct!
try this :
clothesButton1.setDrawingCacheEnabled(true);
clothesButton1.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
clothesButton1.layout(0, 0, clothesButton1.getMeasuredWidth(), imageButton.getMeasuredHeight());
clothesButton1.buildDrawingCache(true);
Bitmap bitmapclothes = Bitmap.createBitmap(clothesButton1.getDrawingCache());
clothesButton1.setDrawingCacheEnabled(false);
Intent intent = new Intent(NowActivity.this,SecondActivity.class);
intent.putExtra("BitmapClothes", bitmapclothes);
startActivity(intent);
Reference:Android View.getDrawingCache returns null, only null

Close another activity from current one

I want to accomplish this structure:
Activity A is showing. If some button is pressed then open activity B (without closing current instance of A). If I press a back button of B I want just to finish B so that I can see my old instance of A. But if in activity B I press another button, I want to close A and open C.
How can I close activity A and start activity C when activity B is opened?
Explanation: When B is active the A mustn't be destroyed so that I could return to it. But if I want to open C then A must be destroyed, so that if I'd press back button of C I wouldn't see it anymore.
I already implemented the code that opens all of the activities by using startActivity() and finish() methods. All I need right now is an answer or suggetion of how could I rework my structure to accomplish my goal.
EDIT
I think I've got an idea to use startActivtyForResult() when I want to open B, so that when I'm ready to open C I'd just let A do this with closing itself.
When you Press Button C go to the ActivityC you just need to pass addFlag method with intent as follows
public void onClick(View v) {
if(v.getId()==R.id.butoonC){
Intent intent = new Intent(this, ActivityC.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
here Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all the activity from activity stack except activity B and activity C. So when u backpress from Activity B you Activity will not able to go back to Activity A.
I hope this work for u
Here is how I've solved the problem:
Activity A:
//Start Activity B
startActivityForResult(new Intent(this, B.class), 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
startActivity(new Intent(this, C.class));
finish();
}
}
Activity B:
//back button press:
setResult(RESULT_CANCELED, new Intent());
finish();
//start Activity C button:
setResult(RESULT_OK, new Intent());
finish();
Hope it will help someone.
I use this and worked for me
in Activity A when a button press to go to Activity B use this code :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
ActivityA.this.finish();
}
});
It is close Activity A and when back button pressed do not come back to this Activity.

Pass a value from a third activity to a first

so I have a doubt, I have 3 activities, in the first one I send some data to the second one, and in the second one I send some data to the third one and finish the second one, is there a way I can finish the third activity and send some data to the first activity from the third?
This is what I'm doing in the first activity.
mStartGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(view.getContext(), GameInProgress.class);
i.putExtra("questionObject", questions);
startActivityForResult(i,DETAIL_REQUEST);
}
});
And in the second one after i get the extra I do this to send data to the third activity:
Intent i = new Intent(view.getContext(), StatisticsActivity.class);
i.putExtra("questionObject", questionInfo);
startActivity(i);
finish();
Now in the third I get the extras and then I:
Intent returnIntent = new Intent();
isCorrect = true;
returnIntent.putExtra("questionCorrection", isCorrect);
setResult(RESULT_OK, returnIntent);
finish();
I though this would work because when I finish the third activity the onActivityResult in the first activity method is working but when I try to access the Extras I get a nullpointerexception, so I don't know, thank you.
Sorry dude but I doubt your procedure of sending data from 3rd to 1st Activity.If data is simple string or integer. You can use Shared Preference Follow link
How to use Shared preference
Edit Shared Preference
Why Don't you also finish First activity and start new Instance of same activity through intent?

Categories

Resources