0

My app works only if there is network connectivity. So, I check for the same on the app startup and resume.

I have a broadcast receiver which listens to network changes.

If there is no Network, I want to show a Simple 'No Network Connectivity' view. The 'No Network Connectivity' contains one text view stating that there is no internet with a sad face.

When Network becomes available, from the broadcast receiver, I want to change the 'No connectivity view' to my 'Dashboard' view.

The 'Dashboard' view has the main business logic and work flow for the app.

So, depending on the Network status, the activity oscillates between the two views.

What is the best way to achieve this?

Case1: Both the views have their own activities. Problem: I need to handle the backpresses and lifecyle events for 'No Connectivity' view which are redundant.

Case2: One activity and use switch views dynamically depending on network status

Case3: Have two layouts(relative layouts) in the same xml file. Turn the visibility on/off depending on the status.

Case4: Switch views dynamically using Inflater.

Thanks in advance.

andy
  • 484
  • 8
  • 21

1 Answers1

0

Use one activity with two fragments:ConnectStatusFragment,DashboardFragment

private void showFramentDepends(){
    if(networkisfine){
        // init your fragmnet
        switchFragment(ConnectStatusFragment);
    }else{
        switchFrament(DashboardFragment);
    }
}

private void switchFragment(Fragment fragment){
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.replace(R.id.content_fragment, fragment);
    ft.commit();
}

and your activity layout xml file should like this:

<FrameLayout
    android:id="@+id/content_fragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
</FrameLayout>
twocity
  • 646
  • 5
  • 11