Last active
March 6, 2017 14:12
-
-
Save seasox/039b07e08e0890961c12a79f0cd367ed to your computer and use it in GitHub Desktop.
Parceler magically wraps non-annotated list items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/text" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Click to crash" | |
android:id="@+id/button" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="org.example.parcelertest"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<activity android:name=".SecondActivity" /> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 25 | |
buildToolsVersion "25.0.2" | |
defaultConfig { | |
applicationId "org.example.parcelertest" | |
minSdkVersion 15 | |
targetSdkVersion 25 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { | |
exclude group: 'com.android.support', module: 'support-annotations' | |
}) | |
compile 'com.android.support:appcompat-v7:25.2.0' | |
compile 'com.android.support.constraint:constraint-layout:1.0.1' | |
testCompile 'junit:junit:4.12' | |
compile 'org.parceler:parceler-api:1.1.6' | |
annotationProcessor 'org.parceler:parceler:1.1.6' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example.parcelertest; | |
import android.annotation.SuppressLint; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.TextView; | |
import org.parceler.Parcels; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
private static final String LIST_KEY = "list"; | |
private static final String FOO_KEY = "foo"; | |
List<Foo> list; | |
@SuppressLint("SetTextI18n") | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
if (savedInstanceState != null) { | |
list = Parcels.unwrap(savedInstanceState.getParcelable(LIST_KEY)); | |
list.get(0).increment(); | |
} else { | |
list = new ArrayList<>(); | |
list.add(new Foo("bar")); | |
} | |
((TextView) findViewById(R.id.text)).setText(list.get(0).getName() + list.get(0).getCounter()); | |
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(MainActivity.this, SecondActivity.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putParcelable(LIST_KEY, Parcels.wrap(list)); // works | |
//outState.putParcelable(FOO_KEY, Parcels.wrap(list.get(0))); // throws | |
} | |
public static class Foo { | |
private String name; | |
private int counter; | |
public Foo(String name) { | |
this.name = name; | |
counter = 0; | |
} | |
public String getName() { | |
return name; | |
} | |
public void increment() { | |
counter += 1; | |
} | |
public int getCounter() { | |
return counter; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example.parcelertest; | |
import android.support.v7.app.AppCompatActivity; | |
public class SecondActivity extends AppCompatActivity { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment