简体   繁体   中英

Refresh SwipeRefreshLayout from another activity

Activity "SubmitListActivity" contains a SwipeRefreshLayout which needs to be refreshed from within "SettingsActivity".


activity_submitlist.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SubmitlistActivity">



<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainerSubmitList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/submitList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

SubmitlistActivity.java

 // this works!
 submitListRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeContainerSubmitList);
 submitListRefreshLayout.setRefreshing(true);

SettingActivity.java

// This throws an error
LayoutInflater inflator = LayoutInflater.from(getApplicationContext());
View layout = inflator.inflate(R.layout.activity_submitlist, null);
SwipeRefreshLayout submitList = layout.findViewById(R.id.swipeContainerSubmitList);
submitList.setRefreshing(true);

Thrown Error:

android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class <unknown>
    Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
        at android.view.LayoutInflater.createView(LayoutInflater.java:645)

Q1 : Is it possible to refresh the swipeRefreshLayout within the SettingsActivity?

Q2 : When pressing back-button, the SubmitListActivity slides in again -> Is there a method I can place inside SubmitListActivity to catch this and put logic behind?

Refresh SwipeRefreshLayout from another activity

Calling method of one activity in another Activity isn't a good practice at all. For your problem follow below solution.

Call submitListRefreshLayout.setRefreshing(true); in your SubmitlistActivity inside your onRestartActivity() method like below ....

 @Override
    protected void onRestart() {
        super.onRestart();
        submitListRefreshLayout.setRefreshing(true);
    }

Note:- Whenever you come back from settingActivity to SubmitlistActivity onRestart() is calling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM