Search code examples
androidandroid-fragmentsandroid-activityandroid-asynctaskandroid-viewpager

Fragment TextView is not able to be updated from parcelable object


In my fragment, I have a textView which I want to set to a specific String. I want to get the String for the textView from an object, which I send to the fragment as a parcelable. I can retrieve the parcelable object and use the object to get the String (when I log it, the correct String is displayed). But when I want to use this to set the textView, the textView doesn't change.

Any ideas why this happens and how I can fix it?

Thanks!

Edit1: I added the activity the fragment is located in to, maybe the error is here?

Edit2: I added the Data class (the parcelable object). But removed the content of the constructor just to keep it easy to read.

public class NavigationFragment extends Fragment {

    private static final String TAG = "NavigationFragment";

    public NavigationFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_navigation, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
       Bundle bundle = this.getArguments();
        if(bundle!=null) {
            Data data = (Data) bundle.getParcelable("data");
            TextView stopTitle = (TextView) view.findViewById(R.id.stopTitle);
            final String name = data.getTourName();
            Log.d(TAG, name);
            stopTitle.setText(name);
        }
    }

}
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    tools:context=".DoTourActivity">

    <TextView
        android:id="@+id/stopTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:text="stopTitle"
        android:textSize="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="365dp"
        android:layout_height="158dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.492"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/stopTitle"
        tools:src="@drawable/placeholder_pic" />

    <TextView
        android:id="@+id/stopDescription"
        android:layout_width="384dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="24dp"
        android:text="stopDescriptionstopDescriptionstopDescription stopDescription stopDescription stopDescription stopDescription stopDescription stopDescription stopDescription stopDescriptionstopDescriptionstopDescription stopDescription stopDescription stopDescription stopDescription stopDescription stopDescription stopDescription"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="395dp"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="8dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/stopDescription">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@android:drawable/ic_menu_mylocation" />

        <TextView
            android:id="@+id/locationAdress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="locationAdress"
            android:textSize="23dp" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@android:drawable/ic_dialog_map" />

    </LinearLayout>

    <EditText
        android:id="@+id/pincode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:inputType="number"
        android:text="pincode"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <Button
        android:id="@+id/confirmButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="confirm"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pincode" />

</android.support.constraint.ConstraintLayout>
public class DoTourActivity extends AppCompatActivity {

    private static final String TAG = "DoTourActivity";

    private SectionStatePagerAdapter sectionStatePagerAdapter;
    private ViewPager viewPager;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_do_tour);

        //Fragment management
        sectionStatePagerAdapter = new SectionStatePagerAdapter(getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(viewPager);

        //Setup actionbar
        Toolbar myToolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(myToolbar);


        //get chosen location & build tour
        Fragment fragment = new NavigationFragment();
        String location = getIntent().getStringExtra("location");
        Bundle bundle = new Bundle();
        Data data = new Data(location);
        bundle.putParcelable("data", data);
        fragment.setArguments(bundle);

        //launch fragment
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.container, fragment);
        transaction.commit();
    }

    public void setupViewPager (ViewPager viewPager) {
        SectionStatePagerAdapter adapter = new SectionStatePagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new NavigationFragment(), "navFragment");
        adapter.addFragment(new QuestionFragment(), "qesFragment");
        viewPager.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        switch(item.getItemId())
        return super.onOptionsItemSelected(item);
    }
}
public class Data implements Parcelable {

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Data createFromParcel(Parcel in) {
            return new Data(in);
        }

        public Data[] newArray(int size) {
            return new Data[size];
        }
    };

    private String tourName;
    int tourID;
    int possiblePoints;
    int stops;
    Spot[] spots;

    //while playing tour
    int points = 0;

    // Constructor
    public Data(String tour){

    }

    public String getTourName() {
        return tourName;
    }

    public void setTourName(String tourName) {
        this.tourName = tourName;
    }

    public int getTourID() {
        return tourID;
    }

    public void setTourID(int tourID) {
        this.tourID = tourID;
    }

    public int getPossiblePoints() {
        return possiblePoints;
    }

    public void setPossiblePoints(int possiblePoints) {
        this.possiblePoints = possiblePoints;
    }

    public int getStops() {
        return stops;
    }

    public void setStops(int stops) {
        this.stops = stops;
    }

    public Spot[] getSpots() {
        return spots;
    }

    public void setSpots(Spot[] spots) {
        this.spots = spots;
    }

    public int getPoints() {
        return points;
    }

    public void setPoints(int points) {
        this.points = points;
    }

    // Parcelling part
    public Data(Parcel in){
        this.tourName =  in.readString();
        this.tourID =  in.readInt();
        this.possiblePoints =  in.readInt();
        this.stops =  in.readInt();
        this.spots = in.createTypedArray(Spot.CREATOR);
        this.points = in.readInt();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.tourName);
        dest.writeInt(this.tourID);
        dest.writeInt(this.possiblePoints);
        dest.writeInt(this.stops);
        for(Spot s : spots){
            dest.writeParcelable(s, flags);
        }
        dest.writeInt(this.points);
    }


}

Solution

  • Thanks to @Mike M. this is solved.

    The problem was with the activity in which the fragments were hosted. In there I created two different NavigationFragments which I added to the activity. The updating of the TextView happen on the wrong one.