-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
weiqiangliu
committed
Dec 10, 2021
1 parent
d8787fd
commit bdcfafc
Showing
6 changed files
with
63 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...lyticsSDK/src/main/java/com/sensorsdata/analytics/android/sdk/util/FragmentCacheUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.sensorsdata.analytics.android.sdk.util; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.os.Build; | ||
import android.text.TextUtils; | ||
import android.util.LruCache; | ||
|
||
import com.sensorsdata.analytics.android.sdk.SALog; | ||
|
||
import java.lang.ref.WeakReference; | ||
|
||
public class FragmentCacheUtil { | ||
|
||
@SuppressLint("NewApi") | ||
private static LruCache<String, WeakReference<Object>> sFragmentLruCache = new LruCache<>(Integer.MAX_VALUE); | ||
|
||
public static void setFragmentToCache(String fragmentName, Object object) { | ||
if (!TextUtils.isEmpty(fragmentName) && null != object && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { | ||
sFragmentLruCache.put(fragmentName, new WeakReference<>(object)); | ||
} | ||
} | ||
|
||
public static Object getFragmentFromCache(String fragmentName) { | ||
try { | ||
if (!TextUtils.isEmpty(fragmentName)) { | ||
WeakReference<Object> weakReference = null; | ||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) { | ||
weakReference = sFragmentLruCache.get(fragmentName); | ||
} | ||
Object object; | ||
if (null != weakReference) { | ||
object = weakReference.get(); | ||
if (null != object) { | ||
return object; | ||
} | ||
} | ||
object = Class.forName(fragmentName).newInstance(); | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { | ||
sFragmentLruCache.put(fragmentName, new WeakReference<>(object)); | ||
} | ||
return object; | ||
} | ||
} catch (Exception e) { | ||
SALog.printStackTrace(e); | ||
} | ||
return null; | ||
} | ||
} |