Run second activity when first is closed - android

Can you tell me how to run my second activity if my first activity is closed?There can be some time before the second activity must be opened.Should I use a Service?And please give an example.Thank you so much.

The answer will depend under what condition you want to start the next activity. Following is just a sample, whatever is done in the onDestroy can be copied anywhere else to start the next activity.
Override the onDestroy method of the first activity and then fire an intent to start the second activity.
In your first activity,
public class A extends Activity{
.
.
.
#Override
public void onDestroy(){
Intent in = new Intent(this, SecondActivity.class);
startActivity(in);
.
.
.
}
}
Make sure both the Activities are metioned in the AndroidManifest.xml
EDIT: To start after some time, you can use a Handler like this. (NOTE : This one is not tested. I hope it works as expected)
public void onDestroy(){
new Handler.postDelayed(new Runnable(){
public void run() {
Intent in = new Intent(this, SecondActivity.class);
startActivity(in);
}
}, 7000);
}
7000 over here is the milliseconds after which you want to start the next activity. i.e the next activity will start after 7s.
All the best :)

if my first activity is closed
For me it sounds like you mean "closed as a result of a user action". Starting a new Activity from onDestroy() is a bad idea since the system may destroy your Activity if it needs to. It can also be destroyed if the device is rotated. A better approach is overriding the back button:
#Override
public void onBackPressed() {
//delay 2nd Activity launch
super.onBackPressed();
}

If you try to delay a launch of a second Activity from within your first Activity, you risk it not opening at all, if the app is cleared from memory. Your best bet is to use AlarmManager to schedule the opening of a new Activity after a certain amount of time. This puts the work on the Android OS instead of in your app.
This post contains some code examples of how to approach this.

Related

Back button from activity 2 closes down the app instead of going to activity 1 in Android 7

Pressing back button from the second activity returned to the first activity before without problems. I then updated to Android 7.
Then the whole app closed when pressing back button from the second activity.
I know that there are threads about this here and I have checked them all. Basically, they say that finish() should be avoided from the first activity.
I don't call finish(), so that is the problem here. It is difficult to solve, because it works like it should when I launch the app from Android studio.
It returns to the first activity from second. The problem occurs when the app is started by pressing its icon (not from Android studio).
Pressing back from the second activity closes down the whole app. How can I solve this? Here is some of my code:
Activity 1:
Intent glIntent = new Intent("astral.worldstriall.GLActivity");
glIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
The below code should be used in 2nd activity so that when u press back button it terminates the current activity(2nd activity) and goes back to previous activity
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
I think you just misused the Intent constructor. According to the documentation, you used this constructor Intent(String action). The one that you atually want should be this one Intent(Context packageContext, Class<?> cls).
In the first activity (therefore this being the instance of your first activity), you should write:
Intent glIntent = new Intent(this, astral.worldstriall.GLActivity.class);
glIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(glIntent);
For going first to second activity...
Intent i=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
Use below code for going in previous activity...
#Override
public void onBackPressed() {
super.onBackPressed();
Intent i=new Intent(SecondActivity.this, FirstActivity.class);
startActivity(i);
}

Passing current Intent as extra to another Activity

I have a problem with my Login screen. When it's started, I check for network connection, and if it's disabled, I want to show NoNetworkActivity. And the same for every other screen: when Activity is launched, I check network connection and navigate to NoNetworkActivity is needed. When navigating, I want to save the Intent which launched this previous activity and finish it to disable the Back button redirection when on NoNetworkActivity. So, when connection is restored, I want to launch that intent and get actual state of the app before this error:
LoginActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (!App.getInstance().isNetworkConnected()) {
Intent noNetwork = new Intent(this, NoNetworkActivity.class);
noNetwork.putExtra(NoNetworkActivity.EXTRA_FAILED_INTENT, getIntent());
startActivity(noNetwork);
finish();
}
...
NoNetworkActivity
private void checkNetworkConnection() {
mCheckButton.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
if (App.getInstance().isNetworkConnected()) {
Intent failedIntent = getIntent().getParcelableExtra(EXTRA_FAILED_INTENT);
startActivity(failedIntent);
finish();
} else {
mCheckButton.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
App.toast("Connection failed");
}
}
And it's getting strange: startActivity(failedIntent) does NOTHING. I've tried to remove finish() from next line, and NoNetworkActivity just stays on top without anything happening.
And one more thing. You can suggest passing Activity actual class names instead of intents, but I realy need Intent. That's because I'm using a lot of starting actions for every activity and a bunch of extras.
Thanks in advance for any help. Cheers!
Very bad approach. Don't use it.
First, you don't need to finish previous activity just to disable Back action. You can override onBackPressed().
Second, you don't need to start parent activity again. Just call a new activity with startActivityForResult(); and override onActivityResult() callback.
Third, but most important. Why do you want to call a new activity just to show 'No Network' message? And what if network won't be re-established? Just create isNetworkEnabled() method and call it when user attempts to get data from the Internet, before sending actual request to server. If no network - notify a user with an alert or toast.
I suggest you use fragments instead of activities first of all.
Using fragments you can set retainInstance(true);
To disable coming back from an activity to the previous :
1)call finish() on that activity
2)
Intent i = new Intent();
i.setClass(this, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);`
It works with an explicit Intent.
In LoginActivity substitute:
noNetwork.putExtra(NoNetworkActivity.EXTRA_FAILED_INTENT, getIntent());
with:
noNetwork.putExtra(NoNetworkActivity.EXTRA_FAILED_INTENT, new Intent(this, LoginActivity.class));
Btw, Alexander Zhak has some good points in his answer

Android intent service onClick, or start new activity and then Asynctask

Many times in android apps, users click a button/view and start a new activity, where the new activity pops up in front of what the user was previously looking at, and loads data.
Would there be any difference from a user perspective if the data started loading (from network or disk or both) when the user clicked the button before the next activity started. And then that data was returned to the new activity in a broadcast receiver.
This is compared to starting the process in the oncreate of the activity. Assuming these network and i/o processes only take milliseconds either way, would it make a difference to the user if the methods were started in onCreate of the new activity, or started in the old activity onClick.
First way, starting I/O and changing views after I/O finishes
//first activity
public void onClick(View v){
startActivity(new Intent(this, NewActivity.class);
}
//NewActivity.class
onCreate(Bundle mBundle){
super.onCreate(mBundle);
setContentView(R.layout.mView);
mObject = networkCall(); //after network call, the view objects in this layout will reflect data from the network call
}
second way, starting the I/O in the first activity
//first activity
public void onClick(View v){
IntentService networkCall = new IntentService();
//start network call
startActivity(new Intent(this, NewActivity.class);
}
//second activity on create just sets the view and also broadcast receiver
My GUESS is that in the split second that it takes for the activity to pop up, the data from the intent service could become available. But at the same time, passing data via intent could take just as long making the benefits marginal
Insight appreciated
In my experience the onCreate() of your new activity is called almost instantly from when you call startActivity(). The new activity doesn't show up right way because it has to take time to render your layout.
You could play around with timings yourself by using the Log.d() function. Something like Log.d(TAG, "This happend at: " + System.currentTimeMillis()); at different points in your code to see when things happen. Watch the LogCat while your apps runs and you can decide for your self which way is better.

How to programmatically force a full app restart? e.g. kill, then start

I have an app that include both a complex service and activity UI. The app runs on multiple devices which communicate with each other over WIFI via the service.
Since the app is a prototype/in-development, I want to add support for a "force restart" that will kill the app and re-launch it clean. There is a lot of shared UI stuff that has gotten gummed up depending on the use case, and it would be easier during testing (I have multiple devices) if I could just touch a button to restart the app completely.
So, does anyone have any suggestions on how to:
1) Force close/stop/kill your own app, from within your app.
2) Set a timer/intent that tells the OS to launch your app before you close/stop/kill it.
Any tips would be appreciated! Thank you.
Use the below code for restart the app:
Intent mStartActivity = new Intent(HomeActivity.this, SplashScreen.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(HomeActivity.this, mPendingIntentId, mStartActivity,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) HomeActivity.this.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
As you can figure out, finish() is what you want to use in order to kill off an activity. A.C.R.'s way would work, however it will only restart your activity, not really kill off the process, and start it back up. If that's what you are looking for, instead of having a dummy Activity that restarts your original Activity, the correct way to do it would be to use flags.
Intent i = new Intent(this, WrapperActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Those flags will clear your back stack all the way down to the first instance of whatever Activity you are creating, it will kill it, and then create a new one. This is essentially what A.C.R's example is doing, but it's much more concise.
If this isn't good enough for you, In order to do this properly, it's quite a bit of work, and requires more advanced knowledge of the Android system. You'd want to create a service that's running in the background (will need to be a separate process if you want the application level state killed) that you could startup when you wanted to kill the app, have the app kill itself, or have the service kill the app for you, and then have the service launch an intent that would start your activity/application back up.
Hope this helps! Best of luck!
To restart my app, I'm simple doing this and it's working:
Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
System.exit(0);
If I don't use both flags, it won't work.
(Using Android 11, API 30, Pixel 3 device)
Try the below code for restarting the application.
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Here is what you need to do:
Create a sticky Service
Kill the app with killProcess call from the Service
The sticky Service will then restart, and you can open your app with an getLaunchIntentForPackage intent
I like to use a Handler to do this off the main UI thread:
private void scheduleForceClose() {
final Handler closeAppHandler = new Handler();
closeAppHandler.post(new Runnable() {
#Override public void run() {
prefs.edit().putBoolean(StringConstants.FORCE_CLOSED_APP, true).commit();
Log.i(TAG, "Gonna force kill the app process completely!");
System.exit(0);
android.os.Process.killProcess(android.os.Process.myPid());
}
});
}
private void scheduleForceOpen() {
final Handler openAppHandler = new Handler();
taskOpenApp = new TimerTask() {
#Override public void run() {
openAppHandler.post(new Runnable() {
#SuppressLint("ApplySharedPref") #Override public void run() {
Intent intent = getPackageManager().getLaunchIntentForPackage("com.company.yourpackagename");
// Reset the force-close flag so that the close timer can begin again
prefs.edit().putBoolean(StringConstants.FORCE_CLOSED_APP, false).commit();
startActivity(intent);
}
});
}
};
// Decide whether we already force-closed the app, so that we don't repeat it
boolean alreadyForceClosed = prefs.getBoolean(StringConstants.FORCE_CLOSED_APP, false);
if (alreadyForceClosed) {
Log.i(TAG, "App process has already been killed, so schedule app relaunch.");
Timer timerOpen = new Timer();
timerOpen.schedule(taskOpenApp, 5000 /* reopen the app after 5 sec */);
}
}
No can do. The operating system decides when to kill an application. Your app can come back to life whether it was truly dead or just hidden.
This is inconvenient, but the platform works like that. You can produce the same user-facing effect by managing your activities, which you can kill and restore.
There might be a more official way to do this, but here's how I would accomplish this.
For example I am going to pretend there are only two Activities, the one you're currently in (I'll call it FirstActivity), and another "helper" Activity (I'll call SecondActivity).
In the first one (the one you want to restart it from), you would have a button that initiates the restart of the app.
restartButton.setOnClickListener(new OnClickListener(){
#Override
onClick(View v){
//when clicked it starts the helper activity and closes the one you're in
startActivity(new Intent(this, SecondActivity.class));
finish(); //or you could use FirstActivity.onDestroy(); if you want it completely dead
}
});
Second Activity: It's entire purpose is so you can basically restart your app from your app (close everything else and then restart it in this)
Class SecondActivity extends Activity{
#Override
onCreate(Bundle savedInstanceState){
...
//it restarts the old activity so it's new and ends this one
startActivity(new Intent(this, FirstActivity.class));
finish(); //or you could use SecondActivity.onDestroy(); if you want it
}
}
That will completely restart the first activity. I'm not sure if it will be as thorough as you wish, but there really isn't another way to do this sort of thing AFAIK.

How to go to previous activity if the activity is killed by Android runtime?

I had a problem with an app, i.e., when I am moving to the previous window the control goes to the splash screen of my app because the previous activity is killed by the Android runtime.
How do I keep the previous activity alive, or how can I create the activity again and access it from the present activity.
You can make use of a default constructor to close and open the screen.
If you want to move between screens:
1-2-3-4
&&
4-3-2-1
Android itself will take care of it.
For example, if you want traverse in a specific way like
4-2-3-1
Android doesn't allow you, so simply create a constructor and assign present activity to it and make use of that variable to close the screen at any time.
For example, here is the activity class
public class One extends Activity
{
private One Screen;
public One()
{
Screen=this;
}
}
When you want close this activity simply use:
Screen.finish();
instead of
this.finish();
When you want invoke new activity of your liking, simply use
Screen.finish();
Intent i = new Intent(One.this, YourActivity.class);
Log.i(TAG, "calling YourActivity Screen");
startActivity(i);
If you want pass any data between the screens, you can make use of
i.putExtra("valuename", actualvalue);
and you can retrive the value using
Intent startIntent = getIntent();
String actualvalue = startIntent.getStringExtra("valuename");
When you have a Splash activity, I think the correct way is closing in and starting the main activity
Intent mainIntent =new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
Then, from your main activity start calling your subactivities.
Why has it killed your main activity? Are you calling finish()?

Categories

Resources