Reputation: 177
I am using an external card library (gabrielemariotti cardlib) in my app. Especially I am using a CardRecyclerView extends RecyclerView and CardArrayRecyclerViewAdapter extends BaseRecyclerViewAdapter. I have two activities Main Activity (with CardRecyclerView) and ProductCard activity
My goal is to save all cards in Main Activity when I returned from another activities (ProductCard) or when I switch on the orientation of MainActivity. Unfortunatelly I don't understand how to save my 'mRecyclerView' in onSaveInstanceState. Can anyone help?
package com.example.dmitry.myfoodbasket;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import it.gmariotti.cardslib.library.cards.actions.BaseSupplementalAction;
import it.gmariotti.cardslib.library.cards.actions.IconSupplementalAction;
import it.gmariotti.cardslib.library.cards.material.MaterialLargeImageCard;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.recyclerview.internal.CardArrayRecyclerViewAdapter;
import it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView;
public class MainActivity extends AppCompatActivity {
final String LOG_TAG = "myLogs";
ArrayList<BaseSupplementalAction> actions;
ArrayList<Card> cards;
MaterialLargeImageCard card;
CardArrayRecyclerViewAdapter mCardArrayAdapter;
CardRecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(LOG_TAG, "onCreate");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
cards.add(card);
mCardArrayAdapter.notifyDataSetChanged();
}
});
cards = new ArrayList<Card>();
card =
MaterialLargeImageCard.with(this)
.setTextOverImage("Italian Beaches")
.useDrawableId(R.drawable.im_beach)
.setupSupplementalActions(R.layout.carddemo_native_material_supplemental_actions_large_icon,actions )
.build();
cards.add(card);
card.setOnClickListener(new Card.OnCardClickListener() {
@Override
public void onClick(Card card, View view) {
Intent intent=new Intent(MainActivity.this,ProductCard.class);
startActivity(intent);
}});
actions = new ArrayList<BaseSupplementalAction>();
IconSupplementalAction t1 = new IconSupplementalAction(this, R.id.ic1);
t1.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(MainActivity.this, " Click on Text SHARE ", Toast.LENGTH_SHORT).show();
}
});
actions.add(t1);
IconSupplementalAction t2 = new IconSupplementalAction(this, R.id.ic2);
t2.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(MainActivity.this," Click on Text LEARN ", Toast.LENGTH_SHORT).show();
}
});
actions.add(t2);
IconSupplementalAction t3 = new IconSupplementalAction(this, R.id.ic3);
t3.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(MainActivity.this, " Карточка удалена ", Toast.LENGTH_SHORT).show();
cards.remove(card);
mCardArrayAdapter.notifyDataSetChanged();
}
});
actions.add(t3);
mCardArrayAdapter = new CardArrayRecyclerViewAdapter(this, cards);
mRecyclerView = (CardRecyclerView) this.findViewById(R.id.carddemo_recyclerview);
mRecyclerView.setHasFixedSize(false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
if (mRecyclerView != null) {
mRecyclerView.setAdapter(mCardArrayAdapter);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 1
Views: 1995
Reputation: 4192
Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity.
To save additional data about the activity state, you must override the onSaveInstanceState() callback method
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState); }
Now you can save specific Strings, ints or event custom Parelable Objects in above Bundle instance. And when your Activity recreates just check if the Bundle onSavedInstance in onCreate() is null or not
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state and populare your RecyclerView again
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
You can replace above with the data you want to save and then re-populate your views like RecyclerView etc with the data you saved in the savedInstance as key-value pair.
Just keep in mind that the Bundle is not meant to store Large amount of data.
Sorry if some code idents are not much clear, its much harder to post answer from mobile.
For much clearer explanation refer to this official guide
Upvotes: 1