I currently have two activites. One is a login activity that after successful login leads to a main activity that contains multiple fragments.
I currently have a USB intent filter and broadcast receiver that checks to see if a USB device is plugged in. Once it detects it a device and I set to be the default app for the device it will relaunch the app even if I am currently in the main activity. The problem is that it bypasses the log in screen activity and crashes as it does not contain the data for the 2nd activity as no log in has occured.
My problem occurs on the fact that app relaunches and resets connection. Is there a way I can make the app not relaunch or just reload the last fragment within the 2nd activity?
<application
android:theme="@style/AppTheme"
android:allowBackup="true"
android:name=".MainApplication"
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".LoginActivity"
android:theme="@style/Theme.Dark">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DeviceRegistrationActivity"
android:theme="@style/Theme.Dark"/>
<activity
android:name=".navDrawer"
android:theme="@style/Theme.Dark">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
The problem is that it bypasses the log in screen activity and crashes as it does not contain the data for the 2nd activity as no log in has occured.
Then you should not be having the login process be in a separate activity. Have the login process be a fragment that is shown by the same activity as the one that is being started by the USB event. If, when that activity starts, it determines that it does not have the authentication information — for whatever reason — it shows the login fragment.
Is there a way I can make the app not relaunch
Not really. Android devices do not have infinite RAM. Your process will be terminated eventually, and anything that you have cached in static data members or other singletons, such as authentication data, will be gone.
or just reload the last fragment within the 2nd activity?
You are certainly welcome to have the 2nd activity show whatever you want it to. However, if you do not have your authentication data, "whatever you want it to" had better be the fragment to get that data.