Skip to content

Commit 4bf1dfd

Browse files
authored
Merge pull request #1725 from AzureAD/nichbop/fix-application-cancelled
Fix APPLICATION_CANCELLED by handling back button press
2 parents 5e3a6a6 + f2bc049 commit 4bf1dfd

File tree

6 files changed

+20
-29
lines changed

6 files changed

+20
-29
lines changed

changelog.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ V.Next
77
- [MINOR] Implement cert loader for both multiple and legacy WPJ data store in PKeyAuth (#1711)
88
- [PATCH] Fix SDK Cancel when using authorization in current task (#1743)
99
- [MINOR] Added support for broadcasting to applications installed on the device. (#1744)
10+
- [PATCH] Fix APPLICATION_CANCELLED by handling back button press (#1725)
1011

1112
Version 4.1.0
1213
----------

common/src/main/java/com/microsoft/identity/common/internal/providers/oauth2/AuthorizationActivity.java

-7
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,4 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
5151
}
5252
setFragment(mFragment);
5353
}
54-
55-
@Override
56-
public void onBackPressed() {
57-
if (!mFragment.onBackPressed()) {
58-
super.onBackPressed();
59-
}
60-
}
6154
}

common/src/main/java/com/microsoft/identity/common/internal/providers/oauth2/AuthorizationFragment.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
package com.microsoft.identity.common.internal.providers.oauth2;
2424

2525
import android.os.Bundle;
26+
import android.view.View;
2627

28+
import androidx.activity.OnBackPressedCallback;
2729
import androidx.annotation.NonNull;
2830
import androidx.annotation.Nullable;
2931
import androidx.fragment.app.Fragment;
@@ -103,9 +105,21 @@ public void onCreate(@Nullable final Bundle savedInstanceState) {
103105
}
104106
}
105107

108+
@Override
109+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
110+
super.onViewCreated(view, savedInstanceState);
111+
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
112+
@Override
113+
public void handleOnBackPressed() {
114+
handleBackButtonPressed();
115+
}
116+
});
117+
}
118+
106119
void finish() {
107120
final String methodName = "#finish";
108121
LocalBroadcaster.INSTANCE.unregisterCallback(CANCEL_AUTHORIZATION_REQUEST);
122+
109123
final FragmentActivity activity = getActivity();
110124
if (activity instanceof AuthorizationActivity) {
111125
activity.finish();
@@ -189,12 +203,8 @@ public void onDestroy() {
189203
super.onDestroy();
190204
}
191205

192-
/**
193-
* NOTE: Fragment-only mode will not support this, as we don't own the activity.
194-
* This must be invoked by AuthorizationActivity.onBackPressed().
195-
*/
196-
public boolean onBackPressed() {
197-
return false;
206+
public void handleBackButtonPressed() {
207+
cancelAuthorization(true);
198208
}
199209

200210
void sendResult(final RawAuthorizationResult.ResultCode resultCode) {

common/src/main/java/com/microsoft/identity/common/internal/providers/oauth2/CurrentTaskAuthorizationActivity.java

-7
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,6 @@ public void onReceive(Context context, Intent intent) {
115115
}
116116
}
117117

118-
@Override
119-
public void onBackPressed() {
120-
if (!mFragment.onBackPressed()) {
121-
super.onBackPressed();
122-
}
123-
}
124-
125118
/**
126119
* This is invoked when an existing activity is re-used and provided with a new intent with additional information
127120
* NOTE: It's important that you use setIntent to update the intent associated with the activity. Otherwise subsequent calls to

common/src/main/java/com/microsoft/identity/common/internal/providers/oauth2/WebViewAuthorizationFragment.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,9 @@ public void run() {
200200
return view;
201201
}
202202

203-
/**
204-
* NOTE: Fragment-only mode will not support this, as we don't own the activity.
205-
* This must be invoked by AuthorizationActivity.onBackPressed().
206-
*/
207203
@Override
208-
public boolean onBackPressed() {
209-
final String methodTag = TAG + ":onBackPressed";
204+
public void handleBackButtonPressed() {
205+
final String methodTag = TAG + ":handleBackButtonPressed";
210206
Logger.info(methodTag, "Back button is pressed");
211207

212208
if (mWebView.canGoBack()) {
@@ -223,8 +219,6 @@ public boolean onBackPressed() {
223219
} else {
224220
cancelAuthorization(true);
225221
}
226-
227-
return true;
228222
}
229223

230224
/**

gradle/versions.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ext {
2121
androidxTestCoreVersion = "1.2.0"
2222
androidxJunitVersion = "1.1.1"
2323
annotationVersion = "1.0.0"
24-
appCompatVersion = "1.0.2"
24+
appCompatVersion = "1.1.0"
2525
browserVersion = "1.0.0"
2626
constraintLayoutVersion = "1.1.3"
2727
dexmakerMockitoVersion = "2.19.0"

0 commit comments

Comments
 (0)