Android 如何在多个活动之间保留 Snackbar?

5

情况:用户已登录,您显示了一个小吃店,上面写着成功登录,然后导航到另一个意图,但问题是当您导航时,小吃店被取消/销毁。我们如何使其跨活动持久化,就像Toast一样,无论导航到哪个活动,它都保持活动和健康,并遵循超时。


使用Fragment能解决这个问题吗?只需要在Activity中切换一个Fragment,Snackbar就会自然地持续存在。我正在努力弄清楚如何在屏幕旋转后保持Snackbar的显示!:( - swooby
是的,我已经意识到了,但对我来说这不是一个选项,因为我的大部分东西都针对某种活动进行了优化。无论如何,你可以尝试使用onpause。 - TheAnimatrix
你需要在那里放入很多逻辑。 - TheAnimatrix
1
是的,我发现在 onResume 中从头开始重建 SnackBar 就可以正常工作。我总是将我的应用程序状态保留在 Activities 之外,因此 Snackbar 在 onResume 中自动填充,就像在自动显示时填充一样。 - swooby
不要在您的情况下使用SnackBar。 - Gabriele Mariotti
如果还有人感兴趣,请研究服务。我知道在UI和服务结合在一起很愚蠢,但是这是可能的,就像你可以在屏幕上持久保存小部件一样。然而,Snackbar必须在服务内创建和销毁,将其作为最后的选择。最好的方法是使用Fragment! - TheAnimatrix
3个回答

4
您可以创建一个类似于 Snackbar 的自定义 Toast:
custom_toast.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toast_layout"
    android:layout_gravity="bottom|center_horizontal"
    android:background="#545454"
    android:gravity="left|fill_vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/toast_text"
        android:layout_gravity="bottom"
        android:textColor="#ffffff"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="8dp" />
</LinearLayout>

并以这种方式显示:

public void showCustomToast(String msg)
{
    //inflate the custom toast
    LayoutInflater inflater = getLayoutInflater();
    // Inflate the Layout
    View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.toast_layout));

    TextView text = (TextView) layout.findViewById(R.id.toast_text);

    // Set the Text to show in TextView
    text.setText(msg);

    Toast toast = new Toast(getApplicationContext());

    //Setting up toast position, similar to Snackbar
    toast.setGravity(Gravity.BOTTOM | Gravity.LEFT | Gravity.FILL_HORIZONTAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show(); 
}

如果你收到一个ERROR/AndroidRuntime(5176): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()错误

将showCustomToast函数中的代码放在这个run函数中

this.runOnUiThread(new Runnable() {
    @Override
    public void run() {

    }

});

谢谢,如果您不需要点击监听器,只需要显示一条消息,那么这是更好的答案。 - Z3R0

0

实际上,SnackBar 不应该是持久的或者堆叠的,因为它们会覆盖屏幕上的其他元素。你可以在 Google Design 中看到这个规则。

但是你可以在这里使用第三方 Material Design 库:rey5137/material

你可以通过调用以下函数之一来显示它:

public void show(Activity activity);

//parent should be FrameLayout or RelativeLayout so SnackBar can algin bottom.
public void show(ViewGroup parent); 

// It only work if SnackBar is already attached to a parent view.
public void show(); 

0

在您的视图组周围创建一个包装器,并将其添加到每个布局中。您还可以使用窗口覆盖。请参见此处


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接