Doubt in android Activity navigation - android

In my application, the first activity takes care of the splash screen/loader and onces it completes the load, a second activity comes up or starts.. Now when a person clicks on back or navigates back, it is supposed to exit the application but it goes tthe loader or splash activity..how can i avoid this ?? Hope my question was clear...

When your application opens second screen aka second activity finish the first activity aka splash screen.
Just use finish() method to achieve this.
Here is an example
Intent intent = new Intent();
intent.setClass(context, Second.class);
startActivity(intent );
//call finish
finish();

when you start second activity after then CurrentActivity.finish() current activity in your case like splash...

Related

Is is able to finish activity jumped by intent?

Intent intent = new Intent('some system ui activty not in my application');
startActivity(intent);
I delay a certain time then jump back to myself activity, However the SystemActivity is still back in history. How to finish it? I tried nohistory ,excludeFromRecents and start my activity with clearTop, but nothing can be done.
The problem is, if the SystemActivty go to some other page by user, I cannot use Intent to jump to the first page of SystemActivity, which maybe make user confused.
If you want the systemActivity to end before returning to the mainActivity try
startActivityForResult instead of startActivity
startActivityForResult(intent, 0);

android Prevent a background app from launching activities in the foreground

I have an activity in my application that does a process and when finished, launches an activity with a normal intent:
Intent intent2 = new Intent();
intent2.setClass(anActivity.this, JustAnotherActivity.class);
startActivity(intent2);
finish();
The problem is that when you are doing the process, if I press the home button and take it to the background, when it finishes and calls the new activity, even if it is in the home of the phone, this new activity jumps to the foreground.
I am looking for the activity to be launched, but if the user is outside the application, it does not come to the foreground.
I guess the use of a FLAG is necessary, but the ones I have tried have not worked.
sorry for my bad english
Thanks.
what if you take care of ActivityLifecycle (OnPause, OnStop).
You can add a flag when the activity goes "OnPause/OnStop" depending what you want. And them perform the intent just if the activity is not "Pause/Stop".
Activity Lifecycle

Clear Android activity stack and start activity

I know this question has been answered in another posts also. But it doesn't solve my issue.
I have 3 screens: A,B and Home. App starts with A, then goes to B and then to Home page. But I want to delete activities A and B when it reaches Home activity, so that when back is pressed app exits.
I tried:
Intent.FLAG_ACTIVITY_CLEAR_TOP. It works fine when back is pressed. App quits. But if I again open the app from background, it starts with screen B.
Intent.FLAG_ACTIVITY_NEW_TASK. This works exactly as I want. But when Home activity starts, there is a sudden glitch in the screen and it is not smooth.
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities on top of home.
Try the following code:
Intent i = new Intent(YourActivity.this, HomeActivity.class);
i2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
You can do this way also, for your splashScreen(A) you can set nohistory=true in AndroidMenifest.xml, and goto Activity(B), and when you are going on Activity Home from Activity(B) finish activity(B) than goto Home activity.
so both of your previous activities will not store in stack.
And on Activity B override onBackPresh() method but remove super.backpresh() in side it. and start your activity A with finish this current activity, it will work.

Back button goes to the parent activity instead going to the phone home screen

I have two activities, A y B. A is the parent activity of B.
A initialise the second one with this code:
Intent intent = new Intent( this, B.class );
startActivity( intent );
this.finish();
and the B goes back to A (like a logout) with this code:
Intent intent = new Intent( B.this, A.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity( intent );
Giving you some more context, the app has a sliding menu and each list item shows a ListFragment within a ListView. On each of those views the back stack has been cleaned and the idea is when the user press onto the back button, he should go to the phone home screen as Google documentation says and when the user press on the app icon from the phone home screen, it needs to re-init where the user was working on.
What the app is currently doing is going to the phone home screen, but when you press onto the app icon, it starts again from the activity A, which is the launch and main activity.
Any idea why?
Try adding after startActivity() finish().
That will make the provius activity to safely close itself.
You use this code to go to phone Home screen
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Override onBackPressed and write the above code. It should work.
According to my understanding, You have 2 activities A & B.
You are starting Activity A & moving to Activity B same timing calling A.this.finish()
It means there is only 1 activity present in your stack which is Activity B.
And when you press "Back" button, it means its closing your app that removing remaining Activity B from stack, so your stack gets empty. So you need to press "Center" button of your device which will keep your app activity B in background (in onStop() state) & when you start your app again so it will open activity B only (by calling onRestart()).
I hope above explanation is highly sufficient to understand above problem.

How to keep layout and its data

I need your help, I have 2 activities A and B where A is the main activity.
Now B starts the activities from A using startActivityForResult() and from B when I finish it will go back to activity A.
This works fine but the actual purpose was like the Gmail application when you go back from B to A and then start activity B again from A, then A need to start activity with its last screen as i left it.
For example: from inbox->label->draft in gmail how to achieve this to keep data/layout as it is.
Maybe android:launchMode="singleInstance" on activity A and B will get it done?
How to switch Activity from 2 activities without losing its data and current state of data
Ok I have got solution that when every time start your application that time only activity will be created and view and put in stack so the next time you start again this activity just open from background so the activity will available in stack and just brought to front using start activity
From A to start ActivityB
Intent intent = new Intent(context,ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Now for start ActivityA from ActivityB
Intent intent = new Intent(getInstance(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
It just brought front while activity start and put the current activity into background

Categories

Resources