Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: OnClickListener unresponsive on Initial Load?

I'm facing a weird issue and I'm not sure what's causing it. Basically, I'm using the ErrorFragment class to display an error, simple right?

The problem has to do with setting the button click listener using setButtonClickListener. When MainFragment is initially loaded, the button does not register any clicks with my Android TV remote.

However, if I reload MainFragment using the back button on the remote, then the button registers the clicks.

Can anyone give me some insight on why this might be happening?

Thanks!

MainActivity.java:

public class MainActivity extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_browse_fragment"
    android:name="com.ui.MainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ui.MainActivity"
    tools:deviceIds="tv"
    tools:ignore="MergeRootFrame" />

MainFragment.java:

public class MainFragment extends DetailsFragment
{
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        setupUIElements();

        if (isConnectingToInternet() == true)
        {
            prepareBackgroundManager();
            loadRows();
            setupEventListeners();
        }
        else
        {
            InternetConnectivityFragment internetError = new InternetConnectivityFragment();

            getFragmentManager().beginTransaction().add(R.id.main_browse_fragment, internetError).commit();
        }
    }

    public class GetInternetStatus extends AsyncTask<Void,Void,Boolean>
    {

        @Override
        protected Boolean doInBackground(Void... params)
        {

            return hasInternetAccess();
        }

        protected boolean hasInternetAccess()
        {
            try
            {
                URL url = new URL("http://www.google.com");

                HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                urlc.setRequestProperty("User-Agent", "Android Application:1");
                urlc.setRequestProperty("Connection", "close");
                urlc.setConnectTimeout(1000 * 30);
                urlc.connect();

                // http://www.w3.org/Protocols/HTTP/HTRESP.html
                if (urlc.getResponseCode() == 200 || urlc.getResponseCode() > 400)
                {
                    // Requested site is available
                    return true;
                }
            }
            catch (Exception ex)
            {
                // Error while trying to connect
                ex.printStackTrace();
                return false;
            }
            return false;
        }
    }

    // Checking for all possible internet connections
    public  boolean isConnectingToInternet()
    {
        Boolean result = false;

        try
        {
            //get the result after executing AsyncTask
            result = new GetInternetStatus().execute().get();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        catch (ExecutionException e)
        {
            e.printStackTrace();
        }
        return result;
    }

    ....
}

InternetConnectivityFragment.java:

public class InternetConnectivityFragment extends android.support.v17.leanback.app.ErrorFragment
{
    private static int TIMER_DELAY = 3000;
    private static final String TAG = "InternetFragment";
    private static final boolean TRANSLUCENT = true;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setErrorContent();
    }

    private void setErrorContent()
    {
        setImageDrawable(getResources().getDrawable(R.drawable.lb_ic_sad_cloud, null));
        setMessage(getResources().getString(R.string.no_internet_message));
        setDefaultBackground(TRANSLUCENT);

        setButtonText(getResources().getString(R.string.retry_connection));

        setButtonClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                MainFragment mainFragment = new MainFragment();

                if (mainFragment.isOnline() == true)
                {
                    getFragmentManager().beginTransaction().remove(InternetConnectivityFragment.this).commit();
                    getFragmentManager().popBackStack();
                }
            }
        });
    }

}

enter image description here

like image 881
Pangu Avatar asked Jun 22 '16 18:06

Pangu


Video Answer


1 Answers

You have android API to check the Network connectivity. Check this for Network Connectivity

Accessing the Process in method isOnline() is not recommended. Atleast in main thread. mainFragment.isOnline() in the click listener may be the cause. Try commenting that out.

like image 103
Viswanath Lekshmanan Avatar answered Oct 30 '22 12:10

Viswanath Lekshmanan