Android onResume not called after finish() command on different activity - java

I have 2 Activities. My first activity calls the second activity. Once a button is clicked in the second activity it calls the finish() method on that activity causing it to return to Activity 1. This is fine, but my problem is that when it returns none of the expected methods are called (onResume(), onRestart(), on Start()). Is there anything that I could be missing? Anything that can inhibit the call? I have logs in each of the methods making sure that they aren't called. I have also added the android:noHistory="false" into my manifest to try solving the problem, but it did not fix it.
Here is the way I call the Activity
intent = new Intent(this, ViewEdit.class);
startActivity(intent);
and here is an example of the setup of the log in the methods
public void onStart(Bundle bundle){
super.onStart();
Log.d(MYACT, "On Start");
}
Thanks for any help.

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

Update an activity after a dialog is dismissed

In my main activity I have an alert that allows users to edit a setting. Once the user selects that setting and it's dismissed I want to call the onCreate() method to refresh the main activity to be updated with the new settings but I'm not sure how to call the main activity from inside the onClick() function.
The dialog is created in a method in the main activity. I could also call a refresh() method I've created inside my class to update everything but that has the same issue of calling a non-static method from a static context.
Try this :
MainActivity.recreate();
The activity will be recreated with new instance so It will recall onCreate again as you needed. It is supported for API 11 or higher.
For APIs lower than 11 use this:
Intent intent = getIntent();
finish();
startActivity(intent);

Start an Activity in full cycle with out showing UI

I have activityB with UI which is working fine as a regular activity with showing UI upon calling from another activityA. Now, I want to call activityB and let the activity complete the entire activity cycle without showing its UI.
So far I had tried:
Set theme in Manifest:
android:theme = "#android:style/Theme.Translucent.NoTitleBar" />
But the problem is I need to setContentView() in ActivityB so that UI element can be recognized in the ActivityB. This ran into error.
Put finish() in activityB onCreate()
The activityB did not complete the full activity cycle. It end at finish().
Any thought about how I can achieve starting an Activity in full cycle without showing its UI.
Thank you
I would suggest you pass some boolean through intent if UI should be shown or not:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean isShowUI = getIntent().getBooleanExtra(EXTRA_IS_SHOW_UI, true);
if (isShowUI) {
setContentView(R.layout.activity_layout);
}
//your onCreate logic
}

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)

Trying to exit in android. Stack is not correctly cleared

I have been spending lots of hours figuring the reason why top of the stack is not cleared yet.
Well I tried the following:
Intent intent = new Intent(ActionBarActivity.this, MainActivity.class);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
And it turned me to the MainActivity. Then when I try to press back button, the program does not exit, but instead it turns me to the first page of ActionBarActivity.
Let me be more specific:
In MainActivity I call ActionBarActivity. In the ActionBarActivity I have a search bar and I enter a query there and just print the value of the query.
If you think how it will work is a below:
MainActivity -> ActionBarActivity1 -> ActionBarActivity2 -> ActionBarActivity3 -> ..
In that ActionBarActivity I have also an option which brings me back to the MainActivity.
So as I said when I run application with the way above, it will bring me to the MainActivity.
Nice so far, but when I press the back button I expect it to exit, but instead it goes to ActionBarActivity1. I doubt the stack is not properly erased.
What should I do in this case. Thanks
I'm assuming you've seen this post: Android: Clear the back stack and it is not what you want
The FLAG_ACTIVITY_NEW_TASK is creating an new task stack. When you hit "back" that is the only activity in that stack, so Android goes to the previous task - which has the calling activity at the top.
If you remove that flag, then MainActivity is already in the task stack and is brought to the top. And "back" removes it and brings you back to the calling activity. So far, that is the expected Android behavior.
EDIT: Since you commented that you only have 2 activities, then one option is to "finish()" ActionBarActivity after it starts MainActivity like this:
startActivity(intentMainActivity);
finish();
To get the behavior you want with many activities or only in certain situations, you need to use SET_RESULT and finish the activities in the stack when you hit the button to go to the MainActivity. In other words, Android assumes you want to keep activities even when new ones start. But in this case you don't, so you have to tell the activity to destroy itself.
i.e. call setResult(DONE) and then use startActivityForResult and check for your "DONE" flag in the onActivityResult method.
then this:
MainActivity -> ActionBarActivity1 -> ActionBarActivity2 -> ActionBarActivity3
-> MainActivity
becomes:
MainActivity -> ActionBarActivity1 -> ActionBarActivity2 -> ActionBarActivity3
-> call "Done"
Destroy ActionBarActivity3
Destroy ActionBarActivity2
Destroy ActionBarActivity1
MainActivity remains
For example, in ActionBarActivity2:
startActivityForResult(intentActionBarActivity3, ActionBarActivity2_ID);
Then in ActionBarActivity3:
public void exitButton(View view) {
setResult(MY_DONE_FLAG);
finish();
}
And back in ActionBarActivity2:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ActionBarActivity2_ID && resultCode == MY_DONE_FLAG) {
setResult(MY_DONE_FLAG, data);
finish();
}
}
And so on with all activities that you want to "close" when that button is pressed.
try
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
insetead of
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
and you should also use it with every intent which leads you to ActionBarActivity.
i mean if you are going back to ActionBarActivity form ActionBarActivity1 then you use it with intent.
then create intent for MainActivity in your so called ActionBarActivity.
Intent intent = new Intent(ActionBarActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Good luck !!!

Categories