code for retry option in snack bar

时间:2017-08-30 20:04:31

标签: android

I have a splash activity where I check if there is any internet connection or not. If not then it will show a // Shrink logo $(document).scroll(function() { if ($(this).scrollTop() >= 50) { $(".logoHomePage").addClass("smallLogo"); } else { $(".logoHomePage").removeClass("smallLogo"); } }); //nav $(".pg1.btn").click(function() { $("body").animate( { scrollTop: $(".page1").offset().top }, 1200 ); }); $(".pg2.btn").click(function() { $("body").animate( { scrollTop: $(".page2").offset().top }, 1200 ); }); message. In body { background-color: red; margin: 0px; padding: 0px; } article { width: 100%; height: 100vh; margin: auto; } .logoHomePage { margin-left: auto; margin-right: auto; margin-top: 25%; } .smallLogo { width: 100%; max-width: 260px; margin: 10px !important; position: fixed; display: block; } nav { z-index: 20; position: fixed; background-color: white; } .page1 { background: green; } .page2 { background: yellow; } I put a retry option when the user clicks on it. It will again check for an internet connection and goto the next Activity.

Error:C:\Users\Luk?\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.1.aar\f61ab9a130e23c5f88348e1bd6936dcf\res\drawable-xhdpi-v4\abc_ic_menu_copy_mtrl_am_alpha.png file not found

Error:C:\Users\Luk?\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.1.aar\f61ab9a130e23c5f88348e1bd6936dcf\res\drawable-hdpi-v4\abc_btn_radio_to_on_mtrl_000.png file not found

Error:C:\Users\Luk?\.gradle\caches\transforms-1\files-1.1\support-compat-26.0.1.aar\62d1a0a0753b75c4dfbf8fbe3e7715ba\res\drawable-xhdpi-v4\notify_panel_notification_icon_bg.png file not found

2 个答案:

答案 0 :(得分:1)

One possible solution to your problem would be this. You had forgotten to call the Snackbar show method, set the Snackbar click

get_template_names

答案 1 :(得分:0)

我以这种方式做了完全相同的事情:

首先将您的主代码放入mainMethod()之类的方法中,然后从onCreate()调用它。

在onCreate()中,调用这样的方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(!isConnectedToInternet(MainActivity.this)){
        showSnackBar("No Internet Connection", (LinearLayout) findViewById(R.id.llmainActivity));
    }

    else {
        mainMethod();
    }
}

方法是这样的

public void mainMethod(){
  //your main method
 }

public void showSnackBar(String string, LinearLayout linearLayout)
{
    Snackbar.make(linearLayout, string, Snackbar.LENGTH_INDEFINITE).
            setAction("Retry", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!isConnectedToInternet(MainActivity.this)){
                        showSnackBar("No Internet Connection",(LinearLayout) findViewById(R.id.llmainActivity));
                    }
                    else mainMethod();
                }
            }).show();
}

private boolean isConnectedToInternet(Context context) {
    ConnectivityManager cm =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}