Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/src/main/java/com/remind101/archexample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class MainActivity extends AppCompatActivity implements MainView {
private static final int POSITION_LOADING = 1;
private static final int POSITION_EMPTY = 2;

private static final String SIS_KEY_PRESENTER_ID = "presenter_id";

private ViewAnimator animator;
private CounterAdapter adapter;

Expand All @@ -32,7 +34,8 @@ protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
presenter = new MainPresenter();
} else {
presenter = PresenterManager.getInstance().restorePresenter(savedInstanceState);
long presenterId = savedInstanceState.getLong(SIS_KEY_PRESENTER_ID);
presenter = PresenterManager.getInstance().restorePresenter(presenterId);
}

setContentView(R.layout.activity_list);
Expand Down Expand Up @@ -81,7 +84,8 @@ protected void onPause() {
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

PresenterManager.getInstance().savePresenter(presenter, outState);
long presenterId = PresenterManager.getInstance().savePresenter(presenter);
outState.putLong(SIS_KEY_PRESENTER_ID, presenterId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.remind101.archexample;

import android.os.Bundle;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.remind101.archexample.presenters.BasePresenter;
Expand All @@ -10,7 +8,7 @@
import java.util.concurrent.atomic.AtomicLong;

public class PresenterManager {
private static final String SIS_KEY_PRESENTER_ID = "presenter_id";

private static PresenterManager instance;

private final AtomicLong currentId;
Expand All @@ -33,16 +31,15 @@ public static PresenterManager getInstance() {
return instance;
}

public <P extends BasePresenter<?, ?>> P restorePresenter(Bundle savedInstanceState) {
Long presenterId = savedInstanceState.getLong(SIS_KEY_PRESENTER_ID);
public <P extends BasePresenter<?, ?>> P restorePresenter(long presenterId) {
P presenter = (P) presenters.getIfPresent(presenterId);
presenters.invalidate(presenterId);
return presenter;
}

public void savePresenter(BasePresenter<?, ?> presenter, Bundle outState) {
public long savePresenter(BasePresenter<?, ?> presenter) {
long presenterId = currentId.incrementAndGet();
presenters.put(presenterId, presenter);
outState.putLong(SIS_KEY_PRESENTER_ID, presenterId);
return presenterId;
}
}