Activity to be closed from that activity itself in Android studio - java

I have 2 Activities, say activity A and activity B. I have to call activity B from the activity A. Now that is done by using Intent. There is some code in the activity A that must be executed after activity B ends. How can that be done?
I use the following code :
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
finish();
How can i accomplish that?

According to Android Docs: "Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity())."
Activity For Result Doc
And here you have a question about Activity For Result where you will find an example.

Related

Finishing an Activity with transition shows the previous Activity from the activity stack first and then transitions to the new Activity

I have 2 Activities in my Android application - Activity A and Activity B.
When the user presses a button on Activity A, he is navigated to Activity B.
I want Activity A to close after going to the next activity and is completely removed from the activity stack.
I tried adding intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); , but this not work as Activity A was still accessible after pressing the back button.
I added finishAffinity(); , this worked, but while transitioning, the previous activity in the activity stacks gets visible and then it goes to Activity B.
Video
(here the application drawer is shown, and then it goes to Activity B) -
Code -
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this);
Intent i = new Intent(MainActivity.this, MainActivity2.class);
startActivity(i, options.toBundle());
finishAffinity();
Any fix so that the previous activity is not visible and activity is also closed ?
EDIT : I tried finish(), finishAfterTransition() and supportFinishAfterTransition() instead of finishAffinity() but still the previous screen is visible.
So I guess there's an issue with finishAfterTransition()
I solved it by adding android:noHistory="true" attribute to the activity (which has to be removed from the stack) in the manifest file solves the issue.
There is no need to use finish() in the java code after this change.
Method 2 -
Refer #Pratyay ' s answer for a way around.
you have to use finish but like this:
startActivity(Your Activity here)
then Finish()
if you do reverse you can see black screen for a short time do this way
#Override
public void onBackPressed() {
finishAffinity();
}
Use this function in Activity B, this will exit app on pressing back button. And don't use any finish(); or finishAffinity(); in Activity A. This will give a smooth transition

Is it possible to relaunch an activity from another activity after the finish() method has been called on it?

Is it possible to relaunch an activity A from activity B after the finish() method has been called from activity A?
how can I go about it in order to achieve this?
Intent intent = new Intent(this, Activityb.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Use this piece of code and just change the activity name accordingly. This will finish your acitivty A and redirect to activity B

Calling startactivityforresult inside activity that has been called by startactivityforresult

I have this issue that I have a list of A and I can add new A, so I startactivityforresult activity with form for A and there is also a button for selecting AType, so I startactivityforresult another activity with AType list.
Problem comes when I do post back result in AType list.
Intent intent = new Intent();
intent.putExtra("aType", aType);
setResult(RESULT_OK, intent);
finish();
And the result is pushed back to A List instead of A Form.
Is this normal behavior or I am doing something wrong?
If this is normal behavior, what can I do to push back result to A Form instead of A List.
If you need more code - I will provide, but I find it now irrelevant.
I have found out that A Form finishes after startactivityforresult call. But why?
Activity1:
Intent intent-=new Intent(this,myclass.class);
startActivityforResult(intent,100);
Override the method OnACtivity result in Activity 1
Activity2:
setResult(RESULT_OK);
finish();
Turned out I have noHistory="true" in AndroidManifest.xml for activity A Form, that is why it was going back to A List Activity

Android Is An Intent Unique To An Activity or Global

I'm curious as to whether an intent is unique to a set activity or not.
For example, my current usage of an intent is to give it some data from Activity 1 and pass it on to Activity 2.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("id", id.getText().toString());
intent.putExtra("weight", weight.getText().toString());
intent.putExtra("zipTo", zipTo.getText().toString());
intent.putExtra("locationId", locationID.getSelectedItem().toString());
intent.putExtra("imageNum", 1);
startActivity(intent);
Now, in MainActivity I can use the following code to retrieve the data passed from the previous activity.
Bundle bundle = getIntent().getExtras();
id = bundle.getString("id");
weight = bundle.getString("weight");
locationID = bundle.getString("locationID");
zipTo = bundle.getString("zipTo");
After doing some work in MainActivity, I need to return to the previous activity that sent us to MainActivity (and I need the data that was originally sent to return).
Do I need to redefine an intent, and do "intent.putExtra" for each value again in MainActivity before sending it to the first Activity? Or is the intent global, and once defined in one activity I can use it in the others with getIntent().getExtras()?
Intents are not global. You only have access to the bundle in the activity you started with the intent passed.
You should use startActivityForResult(intent). This provides the behavior youre looking for.
Getting a result from an activity
The idea is you start the new intent. In this case 'MainActivity'. Once you are done with your logic in MainActivity you use the strategy above to pass a result back to the activity that started MainActivity.
information passed through intents are not global from one activity to another, what is global is shared preferences and information that is metadata which is stored in manifest.xml.

Returning to the proper activity when pressing the BACK button?

I'm creating an app with multiple screens the user will have to navigate through. Specifically, I'm currently working on a set of activities that must link together as follows:
Main Activity -> a button click leads to "CreateCharacterActivity" -> a button click leads to "CharacterMainActivity"
The BACK button on "CharacterMainActivity" should lead back to MainActivity without showing the CreateCharacterActivity again.
This behavior should be similar in other areas of the app, except it should restore the state the activity the BACK button leads to was in before it was paused.
So to simplify, I want it like this.
Activity A -> Activity B -> Activity C
BACK button causes Activity C to return to Activity A without going through Activity B.
I tried doing this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
But this just invokes a new instance of MainActivity. When I then press BACK at that MainActivity instance, it takes me back to CharacterMainActivity.
How can I achieve this? I'm assuming it involves accessing the Activity stack?
When you move from Activity B to Activity C, call finish() at the same time you call startActivity on Activity C. This will remove Activity B from the task stack.
You can start the child activities for result and then finish the child activities with the result also with the same request code.
To start the child activity:
startActivityForResult (Intent intent, int requestCode)
for finish all the child activity
finishActivity (int requestCode)

Categories