Friday, 23 June 2023

ReactNative App Crashes when on WallpaperManager.setbitmap - Android native module

Aim:
Passing a Base64 from react native app to a native module, which will set it as wallpaper.

Currently:
When I am passing a Base64 String, the wallpaper is set successfully but, the app crashes(closes) or resets ( like reopen state, back to app's home screen ).

Problem:
the app crashes after successfully setting the wallpaper.

React Native Code

...
import {NativeModules} from 'react-native';
const {WallpaperMod} = NativeModules;
...
const Page = (...) => {

const base64String = '...'

 useEffect(()=>{
   WallpaperMod.setWallpaper(base64String);  👈 
 },[])

return(...)

export default Page
}

Android module code (WallpaperMod.java)

package com.zenwalls;

...

public class WallpaperMod extends ReactContextBaseJavaModule {
    ReactApplicationContext context;
    WallpaperMod(ReactApplicationContext context) {
        super(context);
        this.context = context;
    }

    @Override
    public String getName() {
        return "WallpaperMod";
    }

    @ReactMethod
    public void setWallpaper(String image64) {
        Log.d("testing", "Print: a"+ image64);
        byte[] decodedString = Base64.decode(image64, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

                wallpaperManager.setBitmap(bitmap, null, false, WallpaperManager.FLAG_SYSTEM);  👈 

                return;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Error

Fatal Exception

FATAL EXCEPTION: main
                                                                                           Process: com.zenwalls, PID: 5202
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zenwalls/com.zenwalls.MainActivity}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.swmansion.rnscreens.ScreenFragment: calling Fragment constructor caused an exception
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3815)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3977)
    at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:6013)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5902)
    at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:71)
    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2374)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:233)
    at android.os.Looper.loop(Looper.java:344)
    at android.app.ActivityThread.main(ActivityThread.java:8249)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:589)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1071)
Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.swmansion.rnscreens.ScreenFragment: calling Fragment constructor caused an exception
    at androidx.fragment.app.Fragment.instantiate(Fragment.java:631)
    at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
    at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483)
    at androidx.fragment.app.FragmentStateManager.<init>(FragmentStateManager.java:85)
    at androidx.fragment.app.FragmentManager.restoreSaveState(FragmentManager.java:2728)
    at androidx.fragment.app.FragmentController.restoreSaveState(FragmentController.java:198)
    at androidx.fragment.app.FragmentActivity$2.onContextAvailable(FragmentActivity.java:149)
    at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99)
    at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:322)
    at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:273)
    at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:45)
    at android.app.Activity.performCreate(Activity.java:8130)
    at android.app.Activity.performCreate(Activity.java:8110)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1343)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3781)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3977) 
    at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:6013) 
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5902) 
    at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:71) 
    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2374) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loopOnce(Looper.java:233) 
    at android.os.Looper.loop(Looper.java:344) 
    at android.app.ActivityThread.main(ActivityThread.java:8249) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:589) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1071) 
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at androidx.fragment.app.Fragment.instantiate(Fragment.java:613)
    at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) 
    at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483) 
    at androidx.fragment.app.FragmentStateManager.<init>(FragmentStateManager.java:85) 
    at androidx.fragment.app.FragmentManager.restoreSaveState(FragmentManager.java:2728) 
    at androidx.fragment.app.FragmentController.restoreSaveState(FragmentController.java:198) 
    at androidx.fragment.app.FragmentActivity$2.onContextAvailable(FragmentActivity.java:149) 
    at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99) 
    at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:322) 
    at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:273) 
    at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:45) 
    at android.app.Activity.performCreate(Activity.java:8130) 
    at android.app.Activity.performCreate(Activity.java:8110) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1343) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3781) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3977) 
    at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:6013) 
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5902) 
    at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:71) 
    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2374) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loopOnce(Looper.java:233) 
    at android.os.Looper.loop(Looper.java:344) 
    at android.app.ActivityThread.main(ActivityThread.java:8249) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:589) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1071) 
Caused by: java.lang.IllegalStateException: Screen fragments should never be restored. Follow instructions from https://github.com/software-mansion/react-native-screens/issues/17#issuecomment-424704067 to properly configure your main activity.
    at com.swmansion.rnscreens.ScreenFragment.<init>(ScreenFragment.kt:54)
    at java.lang.reflect.Constructor.newInstance0(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
    at androidx.fragment.app.Fragment.instantiate(Fragment.java:613) 
    at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) 
    at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483) 
    at androidx.fragment.app.FragmentStateManager.<init>(FragmentStateManager.java:85) 
    at androidx.fragment.app.FragmentManager.restoreSaveState(FragmentManager.java:2728) 
    at androidx.fragment.app.FragmentController.restoreSaveState(FragmentController.java:198) 
    at androidx.fragment.app.FragmentActivity$2.onContextAvailable(FragmentActivity.java:149) 
    at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99) 
    at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:322) 
    at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:273) 
    at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:45) 
    at android.app.Activity.performCreate(Activity.java:8130) 
    at android.app.Activity.performCreate(Activity.java:8110) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1343) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3781) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3977) 
    at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:6013) 
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5902) 
    at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:71) 
    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2374) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loopOnce(Looper.java:233) 
    at android.os.Looper.loop(Looper.java:344) 
    at android.app.ActivityThread.main(ActivityThread.java:8249) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:589) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1071) ```


  [1]: https://i.stack.imgur.com/ymWCh.jpg


from ReactNative App Crashes when on WallpaperManager.setbitmap - Android, native module

No comments:

Post a Comment