This is the code which i have written.i could not able to find when this functionality crashes but it works sometimes.
public class ListFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main();
IntentFilter filter1 = new IntentFilter("com.venkat.listfragment");
registerReceiver(myReceiver, filter1);
// register reciever intent filter.
}
public void main(){
IntentFilter filter1 = new IntentFilter("com.venkat.listfragment");
registerReceiver(myReceiver, filter1);
List_Fragment ls_fragment = new List_Fragment();
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, ls_fragment).addToBackStack(null).commitAllowingStateLoss();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
}
private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
main();
}
};}
I have four tabs,When i click on one tab it displays the corresponding screen and from that screen i am navigating another screen with in the same tab.but to get back to the previous screen when i clicked on same tab i have written a broadcast receiver for that.
i am getting this error:
08-19 10:13:51.436: E/AndroidRuntime(2830): FATAL EXCEPTION: main
08-19 10:13:51.436: E/AndroidRuntime(2830): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.venkat.listfragment } in com.iheartsalesevent.main.ListFragmentActivity$1@44a89218
08-19 10:13:51.436: E/AndroidRuntime(2830): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:905)
08-19 10:13:51.436: E/AndroidRuntime(2830): at android.os.Handler.handleCallback(Handler.java:587)
08-19 10:13:51.436: E/AndroidRuntime(2830): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 10:13:51.436: E/AndroidRuntime(2830): at android.os.Looper.loop(Looper.java:123)
08-19 10:13:51.436: E/AndroidRuntime(2830): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-19 10:13:51.436: E/AndroidRuntime(2830): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 10:13:51.436: E/AndroidRuntime(2830): at java.lang.reflect.Method.invoke(Method.java:521)
08-19 10:13:51.436: E/AndroidRuntime(2830): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-19 10:13:51.436: E/AndroidRuntime(2830): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-19 10:13:51.436: E/AndroidRuntime(2830): at dalvik.system.NativeStart.main(Native Method)
08-19 10:13:51.436: E/AndroidRuntime(2830): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
08-19 10:13:51.436: E/AndroidRuntime(2830): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
08-19 10:13:51.436: E/AndroidRuntime(2830): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1354)
08-19 10:13:51.436: E/AndroidRuntime(2830): at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:493)
08-19 10:13:51.436: E/AndroidRuntime(2830): at com.venkat.main.ListFragmentActivity.main(ListFragmentActivity.java:65)
08-19 10:13:51.436: E/AndroidRuntime(2830): at com.venkat.main.ListFragmentActivity$1.onReceive(ListFragmentActivity.java:89)
08-19 10:13:51.436: E/AndroidRuntime(2830): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:892)
08-19 10:13:51.436: E/AndroidRuntime(2830): ... 9 more
I referred so many links related to this issue but those are not resolve my problem.
Registering BroadcastReceiver in onResume()
and unregistering in onPause()
will solve the issue. If needed, you can use onStart()
/ onStop()
as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With