Skip to content

Commit

Permalink
fix(bug): 综合解决有无回调的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Apr 9, 2024
1 parent c6ee361 commit f89f001
Show file tree
Hide file tree
Showing 11 changed files with 570 additions and 33 deletions.
6 changes: 3 additions & 3 deletions activityresult/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

api 'androidx.appcompat:appcompat:1.1.0'
api 'androidx.activity:activity:1.1.0'
api 'androidx.fragment:fragment:1.1.0'
api 'androidx.appcompat:appcompat:1.2.0'
api 'androidx.activity:activity:1.2.0'
api 'androidx.fragment:fragment:1.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hss01248.activityresult;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.hss01248.transfrag.BaseTransFragment;
Expand All @@ -31,22 +33,32 @@
* 2023-06-02 09:51:54.975 3295-3295/com.hss01248.transfragmentdemo D/frag: onDestroy:GoOutOfAppForResultFragment{993ff35} (579cc2d3-2ea6-4aea-93c0-e75343cdeccc tag=25328484-3fe6-4b3c-9061-9be1fbc79070)
* 2023-06-02 09:51:55.025 3295-3295/com.hss01248.transfragmentdemo D/DecorView[]: onWindowFocusChanged hasWindowFocus true
*/
public class GoOutOfAppForResultFragment extends BaseTransFragment<Intent> {
public class GoOutOfAppForResultFragment extends Fragment {

int requestCode;
ActivityResultListener listener;
boolean startWaitingResult;
boolean consumed;
boolean firstOnResume2 = true;

protected FragmentActivity activity;
protected Intent bean;
boolean firstResume = true;
public static boolean debugable = true;

public GoOutOfAppForResultFragment() {
super();
}

public GoOutOfAppForResultFragment(FragmentActivity activity, Intent intent) {
super(activity, intent);
this();
this.activity = activity;
this.bean = intent;
BaseTransFragment.startFragmentTransaction(activity,this);
}

public void goOutApp(ActivityResultListener listener){

requestCode = new Random().nextInt(8799);
this.listener = listener;
if (StartActivityUtil.debugable) {
Expand All @@ -56,35 +68,75 @@ public void goOutApp(ActivityResultListener listener){
if(!listener.onInterceptStartIntent(this,bean,requestCode)){
startActivityForResult(bean,requestCode);
}

}catch (Throwable throwable){
listener.onActivityNotFound(throwable);
finish();
BaseTransFragment.finish(this);
}
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
firstOnResume2 = true;
setRetainInstance(true);
if(debugable){
Log.d("frag","onCreate:"+this);
}
}

@Override
public void onStop() {
super.onStop();
startWaitingResult = true;
if(debugable){
Log.d("frag","onStop:"+this);
}
}

//todo 一些半屏activity,只触发fragment的onPause, 不触发onStop
/* @Override
long lastPause = 0;
@Override
public void onPause() {
super.onPause();
if(debugable){
Log.d("frag","onPause:"+this);
}
lastPause = System.currentTimeMillis();
//对于打开通知权限这种,没有回调的,就会在onpause后立刻回调onActivityResult
//如何处理? --> 在onStop后开始响应onActivityResult
startWaitingResult = true;
}

@Override
public void onResume() {
if(debugable){
Log.d("frag","onresume:"+this);
}
super.onResume();
if(startWaitingResult && !consumed){
onStartOfResultBack(requestCode,66,null);
startWaitingResult = false;

if(firstResume){
onFirstResume();
firstResume = false;
}
}
@Override
public void onDestroy() {
super.onDestroy();
if(debugable){
Log.d("frag","onDestroy:"+this);
}
}*/
BaseTransFragment.finish(this);
}


protected void onFirstResume() {

}


//todo 一些半屏activity,只触发fragment的onPause, 不触发onStop, 比如删除图片的reqeust弹窗.
//todo onActivityResult比onResume先触发

/**
* 如果真有,那么比onStart()先执行
* @param requestCode
Expand All @@ -94,6 +146,15 @@ public void onResume() {
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(debugable){
Log.d("frag","onActivityResult0:"+this);
}
if(System.currentTimeMillis() - lastPause < 500){
if (StartActivityUtil.debugable) {
Log.w("onActivityResult", "onActivityResult和onpause间隔太短,是无效的:" + requestCode + ",result:" + resultCode + ",data:" + data);
}
return;
}
if(startWaitingResult){
consumed = true;
if (StartActivityUtil.debugable) {
Expand All @@ -113,7 +174,11 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent d
public void onStart() {
super.onStart();
if (StartActivityUtil.debugable) {
Log.i("frag", "on start frag req:" + requestCode + ","+this.toString() );
Log.i("frag", "on start frag req:" + requestCode + ","+firstOnResume2+"-"+this.toString() );
}
if(firstOnResume2){
firstOnResume2 = false;
return;
}
//有时,onStart会在onActivityResult之前走,所以要等一会儿
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
Expand Down Expand Up @@ -144,7 +209,7 @@ protected void onStartOfResultBack(int requestCode, int resultCode, @Nullable In
if (StartActivityUtil.debugable) {
Log.i("onActivityResult2", "frag req:" + requestCode + ",result:onStartOfResultBack,data:"+data );
}
finish();
BaseTransFragment.finish(this);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.hss01248.activityresult;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.hss01248.transfrag.BaseTransFragment;
Expand All @@ -15,14 +17,22 @@
* author:hss
* desription:
*/
public class InAppResultFragment extends BaseTransFragment<Intent> {
public class InAppResultFragment extends Fragment {

protected FragmentActivity activity;
protected Intent bean;
boolean firstResume = true;
public static boolean debugable = true;

public InAppResultFragment() {
super();
}

public InAppResultFragment(FragmentActivity activity, Intent intent) {
super(activity, intent);
this();
this.activity = activity;
this.bean = intent;
BaseTransFragment.startFragmentTransaction(activity,this);
}
ActivityResultListener listener;
int requestCode;
Expand Down Expand Up @@ -51,7 +61,7 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent d
if(listener != null){
listener.onActivityResult(requestCode,resultCode,data);
}
finish();
BaseTransFragment.finish(this);
if(requestCode == this.requestCode){
//listener.onActivityResult(requestCode,resultCode,data);
//finish();
Expand All @@ -60,7 +70,65 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent d
Log.w("onActivityResult", "reqcode not same:" + requestCode + ",this.requestCode:" + this.requestCode );
}
}
}


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if(debugable){
Log.d("frag","onCreate:"+this);
}
// firstResume = true;
}

@Override
public void onStart() {
super.onStart();
if(debugable){
Log.d("frag","onStart:"+this);
}
}

@Override
public void onResume() {
if(debugable){
Log.d("frag","onresume:"+this);
}
super.onResume();
if(firstResume){
onFirstResume();
firstResume = false;
}
}


protected void onFirstResume() {

}

@Override
public void onPause() {
super.onPause();
if(debugable){
Log.d("frag","onPause:"+this);
}
}

@Override
public void onStop() {
super.onStop();
if(debugable){
Log.d("frag","onStop:"+this);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if(debugable){
Log.d("frag","onDestroy:"+this);
}
BaseTransFragment.finish(this);
}
}
11 changes: 7 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
compileSdkVersion 33
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.hss01248.transfragmentdemo"
minSdkVersion 16
targetSdkVersion 29
minSdkVersion 24
targetSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -37,7 +37,10 @@ dependencies {
implementation project (':activityresult')
implementation project (':transactivity')
api 'com.blankj:utilcodex:1.30.6'
implementation "androidx.multidex:multidex:2.0.1"
api 'com.github.hss01248.MyPermission:permission:2.2.9'
api('com.github.skyNet2017.Luban:dataForPhotoSelet:3.3.0')
api 'com.github.getActivity:XXPermissions:11.6'
//api "com.github.hss01248.MyPermission:location:2.2.4"
}
configurations {
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hss01248.transfragmentdemo">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:name=".BaseApp"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Loading

0 comments on commit f89f001

Please sign in to comment.