App restarts main activity from background instead of resuming previous state - java

If I launch the app from the home screen (by taping the app icon), then I navigate through the app and then I leave the app, when I reopen the app the same way (by taping the app icon from the home screen) it will resume the previous state and show the last activity I was on before leaving the app.
That part works as expected.
Here is the issue:
If I first launch the app from the play store or manually from the apk installer, and then reopen the app another way (by taping the app icon from the home screen for instance), the app will start a new instance of the main activity and will add it the previous navigation stack (If I press the back button it will go back to the last activity I was on before leaving the app).
I always want the app to open the last activity from background, no matter how the app was first launched (via the home screen or the play store or manually).
I already tried stuff like that in the main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
[...]
}
but the finish() call crashes the app.

I actually found why finish() crashed the app: onDestroy() was called and it tried to unregister a receiver that was not registered yet, so the app crashed: Unable to destroy activity [...] Receiver not registered.
So that code actually works fine for my issue:
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
I just didn't pay attention to onDestroy()

Related

react-native-notifications Notification opened when app is opened from app tray (Android only)

I am using react-native-notifications version 4.2.4 on React Native 0.64.1. I have also tried on react-native-notifications version 4.3.1 on React Native 0.67.
Steps
Hard close the app.
Open a notification. getInitialNotification() is
populated. The app will open the first screen, then navigate to a
screen with the notification details.
Press the hardware back button
twice (at this point the app has been exited)
Open the app from the app tray.
Expected Result
onNotificationOpened() event listener is not fired and the first screen of the app is shown
Actual Result
onNotificationOpened() event listener is fired and step 2 happens.
If I comment out the call to callOnOpenIfNeed() in onActivityStarted() in RNNotificationsPackage.java, this only happens the first time after the app is hard closed. I believe that this function is causing the issue to happen and notification.onOpened() is being called incorrectly as the Intent.getExtras() should not have the notification object.
See below for reference:
private void callOnOpenedIfNeed(Activity activity) {
Intent intent = activity.getIntent();
if (NotificationIntentAdapter.canHandleIntent(intent)) {
Context appContext = mApplication.getApplicationContext();
Bundle notificationData = NotificationIntentAdapter.cannotHandleTrampolineActivity(appContext) ?
NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent) : intent.getExtras();
final IPushNotification pushNotification = PushNotification.get(appContext, notificationData);
if (pushNotification != null) {
pushNotification.onOpened();
}
}
}
I have seen this on Android versions 9-12.

Skip a page on back button press if network is available android studio

I'm new to Android, working on a webview app. I have created a custom HTML error page in asset folder for loading when internet not available, it is working fine.
My issue is when internet is back, after browsing different screen on back button press it shows the error page also, how to skip this.
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
DashActivity.super.onBackPressed();
}
}
In the no internet activity, when you open another activity on getting the internet connection back, you are forgetting to finish() the activity. Due to that, the activity opens again.
There can be error because you didn't finish the activity so.
You should just write finish() instead of DashActivity.super.onBackPressed()

How to lock Android device after unlocking with setShowWhenLocked(true);

I am creating a video calling app and have the following code which is called when the application receives a push notification - it unlocks the screen and presents an 'incoming call' user interface:
public class MainActivity extends ReactActivity {
#Override
protected String getMainComponentName() {
return "x";
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
}
This works fine when a call is incoming - the user can interact with the app using the UI presented. However, the problem is is that if the app is in the foreground and the phone is then locked, when the unlock button is pressed on the side of the phone, the app is displayed, instead of the keyguard / lock screen being displayed. It permanently allows access to the app if it is in the foreground and the phone is locked and the unlock button is pressed.
I want the app to appear when the it receives a push notification and the screen is locked, but I also want the user to be able to lock the device fully and not give the user access to the app after pressing the lock button.
How can I achieve this?
As far as I know the best solution to that problem is having multiple types of activities.
In the first activity (calling activity) you set setShowWhenLocked and setTurnScreenOn to true (like you did).
When a call comes in, you start the calling activity which handles the call, because you set the two attributes, the activitiy will be shown to the user even if the device is locked.
Furthermore, while the call activitiy is active, the user will be able to "lock" his device (i.e. press the power button), and when he unlocks he will be presented with the calling activity again (without entering the code).
This is the same behaviour as most default android calling/phone apps have.
The second activity is used for your other logic, that should only be accessible when the user really unlocks his phone. (i.e. enter the code)
So, when the phone call is ended, you start the second activity from the calling activity, e.g. like this:
Intent intent = new Intent(this, SecondActivity.class);
this.startActivity(intent);
where this is the instance of the calling activity
The just started SecondActivity will then automatically be locked behind the lockscreen (i.e. not accessible without unlock) if the phone was locked before.
I just tested this behaviour in a small test project, if you need further assistance, just ask.
In my VoIP app (baresip), I solved the problem by calling requestDismissKeyguard() function also when power button is pressed to unlock the device. That can be detected by registering a broadcast receiver with IntentFilter(Intent.ACTION_SCREEN_ON). Then if the device is locked with secured keyguard, requestDismissKeyguard() will ask for a PIN code or password before the user can access the app.

How do I refresh Webview everytime the app is brought back from background?

I have a webview application, and I need to refresh(using webview.Reload()) the webview whenever the app has been running in the background (has not been killed) and is brought back.How would I do that?
As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.
#Override
public void onResume() {
super.onResume();
webView.reload();
}
https://developer.android.com/guide/components/activities/activity-lifecycle

App crashes after tapping the home button and then returning to the same activity by clicking the app icon

When I launch my activity after having installed it, it runs fine; then if I tap the home button of the device (I'm using Lenovo TB3 tablet), and then launch the activity again by clicking on its icon, the app crashes and relaunch itself automatically.
I am not able to figure out the issue.
My Stack Trace:
App has crashed
java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.IllegalStateException: Already added!
I have checked the life cycle of my application,
when I press back button of my device following methods were called
onCreate()
onStart()
onResume()
And when I pressed the back button following methods were called
onPause()
onStop()
onDestroy()
And, Same above methods were called when pressing the home button.
Then why the app crashes by clicking on it's icon after pressing the home button?
MyActivity Code:
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate:savedInstanceState" +savedInstanceState);
super.onCreate(savedInstanceState);//At this line crash happens
setContentView(R.layout.basic_activity_concept_home);
Logger.d(TAG, "onCreate: ");
ButterKnife.bind(this);
student = getIntent().getParcelableExtra(Constants.JSON_KEY_STUDENT);
pageIndex = 0;
pager.addOnPageChangeListener(onPageChangeListener);
setPresenter(presenter);
showProgress(R.string.getting_concept_activities);
}
Code That calls the above activity:
#OnClick(R.id.btn_concept_basic_activities)
public void goToConceptActivities() {
Intent intent = new Intent(this, BasicActivityConceptHome.class);
intent.putExtra(Constants.JSON_KEY_STUDENT, student);
startActivity(intent);
}
I didn't find any workaround of this issue.
Any help would be much appreciated. :)
Thanks :)
This can happen if your activity is declared as singleTask or singleInstance in Manifest.

Categories

Resources