Activity transition with JAVA class refactoring and instance experimented on Android side

background

This text may be helpful for those who want the following two specifications.

1. Object type refactoring: Subclass-> Parent class-> Subclass conversion, the contents of the instance are not actually shrunk or lost. (Figure 1)
2. If there is an array that you want to pass when the activity transitions, you can pass it to the next activity using ordinary putExtra (String name, Serializable value) by implementing the Serializable interface for the class of the object stored in the array. (Figure 2)

スクリーンショット 2018-02-06 17.10.16.png Figure 1

スクリーンショット 2018-02-06 17.19.59.png Figure 2

Source code

1.Contribution.java


public class Contribution implements Serializable{
    String category;
    Date time;
    String title;
    String content;

    public Contribution(){
        category = "general";
        time = new Date();
        title = "general";
    }


    public String getCategory() {
        return category;
    }

    public String getTime() {
        return time.toString();
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


    public String toString() {
        return "Contribution-->Title:   " + getTitle() + "\nCategory:   " + getCategory() +"\nContent:n" + getContent() + "\nDate:   " + getTime();
    }



}

2.MyContribution.java

public class MyContribution extends Contribution{
    public String subTitle;

    public MyContribution(String _title,String _content,String _subTitle){
        title = _title;
        content = _content;
        subTitle = _subTitle;
        category = "training";
    }

    public String getSubTitle() {
        return subTitle;
    }

    public String toString(){
        return "MyContribution-->Title:   " + getTitle() + "\nsubtitle:   " + getSubTitle() + "\nCategory:   " + getCategory() +"\nContent:\n" + getContent() + "\nDate:   " + getTime();
    }
}

3.TypeConvertActivity.java

public class TypeConvertActivity extends AppCompatActivity {
    Contribution contribution = null;
    ArrayList<MyContribution> myContributionArrayList;
    ArrayList<Contribution> contributionArrayList;
    ArrayList<MyContribution> myRecoveryContributionArrayList;

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

        MyContribution myContribution = new MyContribution("JAVA basics","About development tips","JAVA basics開発編");
        contribution = (Contribution) myContribution;

        myContributionArrayList = new ArrayList<MyContribution>();
        contributionArrayList = new ArrayList<Contribution>();
        myRecoveryContributionArrayList = new ArrayList<MyContribution>();

        myContributionArrayList.add(new MyContribution("Java Study1","new String('good day')","String Type"));
        myContributionArrayList.add(new MyContribution("Java Study2","new Boolean(true)","Boolean Type"));
        myContributionArrayList.add(new MyContribution("Java Study3","new Integer(17)","Integer Type"));
        myContributionArrayList.add(new MyContribution("Java Study4","new ArrayList<>()","ArrayList Type"));
        myContributionArrayList.add(new MyContribution("Java Study5","new Double(3.1415926)","Double Type"));


        for(MyContribution m:myContributionArrayList){
            Log.d("myContribution",m.toString());
            contributionArrayList.add((Contribution) m);
        }

        for(Contribution m:contributionArrayList){
            Log.d("Contribution",m.toString());
            myRecoveryContributionArrayList.add((MyContribution) m);
        }

        for(MyContribution m:myRecoveryContributionArrayList){
            Log.d("Recovery Contribution",m.toString());
        }


        Button bringContributionToNextPage = (Button) findViewById(R.id.bringContributionToNextPage);
        bringContributionToNextPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToActivity();
            }
        });

    }

    private void goToActivity(){
        Intent intent = new Intent(getApplication(),ReceiveTypeConvertActivity.class);
        intent.putExtra("contribution", contribution);
        intent.putExtra("contributionArrayList",contributionArrayList);
        startActivity(intent);
    }


}

4.activity_type_convert.xml

<?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"
    tools:context="com.example.syoui.imagetab.java_knowledge.TypeConvertActivity">

    <Button
        android:id="@+id/bringContributionToNextPage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BringContributionToNextPage"/>
</android.support.constraint.ConstraintLayout>

5.ReceiveTypeConvertActivity


public class ReceiveTypeConvertActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_type_convert);
        Intent intent = getIntent();
        Serializable contributionSerial = intent.getSerializableExtra("contribution");
        if(contributionSerial != null){
            Contribution contribution = (Contribution) contributionSerial;
            Log.d("Receive:contribution",contribution.toString());
        }

        Serializable contributionArrayListSerial = intent.getSerializableExtra("contributionArrayList");
        if(contributionArrayListSerial != null){
            ArrayList<Contribution> contributionArrayList = ( ArrayList<Contribution>)contributionArrayListSerial;
            for(Contribution m:contributionArrayList){
                Log.d("contributionArrayList",m.toString());
            }
        }


    }
}

6.activity_receive_type_convert.xml

<?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"
    tools:context="com.example.syoui.imagetab.java_knowledge.ReceiveTypeConvertActivity">

</android.support.constraint.ConstraintLayout>

3. Result

After checking the output of the log, even if I cast the instance to a class that does not have a Contribute class (subtitle), the toString () method is also executing the toString () method of the MyContribution class. Cast an instance of MyContribution to the parent class Contribution once, but the contents do not change. スクリーンショット 2018-02-06 17.32.55.png Figure 3 Also, the recipient of the contributionArrayList received the entire contributionArrayList without any problem. スクリーンショット 2018-02-06 17.46.34.png Figure 4

It's not a big deal, but it's a problem you can encounter at work.

Recommended Posts

Activity transition with JAVA class refactoring and instance experimented on Android side
[Java] Differences between instance variables and class variables
[Android / Java] Screen transition and return processing in fragments
Compile with Java 6 and test with Java 11 while running Maven on Java 8
I want to transition screens with kotlin and java!
[Java Silver] What are class variables instance variables and local variables?
[Java] A class is an OS, and an instance is a virtual computer.
Java class definition and instantiation
Difference between class and instance
Screen transition with swing, java
Search and execute method by name from instance with processing (java)
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
How to call with One Touch (without confirmation) on Android (Java)