Recreate activity passing hardcoded null instance state - android

I am making a puzzle game and every time the user completes the puzzle, a recreate button appears which is simply calling the recreate() method to restart the puzzle activity.
I override onSaveInstanceState because i want to save the image selected for the puzzle and the 4 pieces in case of screen orientation change.
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("originalBM", originalBm);
outState.putParcelable("bm1", bm1);
outState.putParcelable("bm2", bm2);
outState.putParcelable("bm3", bm3);
outState.putParcelable("bm4", bm4);
}
So, when the user clicks the recreate button, the recreate() method is being called which also calls onSaveInstanceState by default because this is how android works and the user will have to play the puzzle with the same image again and again.
I don't want to implement the same code that i have on my onCreate method to select a new random image because this is causing memory leaks and my app crashes after 10-12 recreates.
I simply want it to restart the activity clean and fresh!
Instead of using recreate() inside my recreatePuzzle method, I also also tried this
Intent intent = getIntent();
finish();
startActivity(intent);
But this again is causing my app to crash after 10-12 recreates. It is also causing memory leaks.
So, i believe the best way to do this would be by skipping the Override of saveInstanceState when my recreatePuzzle is being called (if this is possible) or by passing a null Bundle when onSaveInstanceState is being called.
Is there any way to implement any of these solutions above?
Any help would be highly appreciated.
Thank you all in advance.
EDIT:
Full code of my class
package kidsbook.jok.kidsbook;
public class Puzzle extends AppCompatActivity {
private String[] puzzleIMGS;
private String randomPuzzleIMG;
private int corrects = 0, tries = 0;
private ImageView part1, part2, part3, part4;
private TextView piece1, piece2, piece3, piece4;
private Button againButton;
private Bitmap bm1, bm2, bm3, bm4, originalBm;
private Intent i;
private MediaPlayer mp = new MediaPlayer();
private List<Bitmap> parts = new ArrayList<>();
private boolean recreatePuzzle = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_puzzle);
mp = MediaPlayer.create(getApplicationContext(), R.raw.pop);
//Select random image
puzzleIMGS = getResources().getStringArray(R.array.all_animal_imgs);
randomPuzzleIMG = puzzleIMGS[new Random().nextInt(puzzleIMGS.length)];
//Get all elements
againButton = (Button) findViewById(R.id.againPuzzleButton);
part1 = (ImageView) findViewById(R.id.part1);
part2 = (ImageView) findViewById(R.id.part2);
part3 = (ImageView) findViewById(R.id.part3);
part4 = (ImageView) findViewById(R.id.part4);
piece1 = (TextView) findViewById(R.id.piece1);
piece2 = (TextView) findViewById(R.id.piece2);
piece3 = (TextView) findViewById(R.id.piece3);
piece4 = (TextView) findViewById(R.id.piece4);
part1.setOnTouchListener(new MyTouchListener());
part2.setOnTouchListener(new MyTouchListener());
part3.setOnTouchListener(new MyTouchListener());
part4.setOnTouchListener(new MyTouchListener());
piece1.setOnDragListener(new MyDragListener());
piece2.setOnDragListener(new MyDragListener());
piece3.setOnDragListener(new MyDragListener());
piece4.setOnDragListener(new MyDragListener());
if(savedInstanceState!=null) {
Log.i("debug","inside saved instance");
//Convert randomly selected resource image to bitmap
originalBm = savedInstanceState.getParcelable("originalBM");
bm1 = savedInstanceState.getParcelable("bm1");
bm2 = savedInstanceState.getParcelable("bm2");
bm3 = savedInstanceState.getParcelable("bm3");
bm4 = savedInstanceState.getParcelable("bm4");
} else {
Log.i("debug","inside null instance");
//Convert randomly selected resource image to bitmap
originalBm = BitmapFactory.decodeResource(getResources(), getImageId(this, randomPuzzleIMG));
//Split bitmap to 4 parts
bm1 = Bitmap.createBitmap(originalBm, 0, 0, (originalBm.getWidth() / 2), (originalBm.getHeight() / 2));
bm2 = Bitmap.createBitmap(originalBm, (originalBm.getWidth() / 2), 0, (originalBm.getWidth() / 2), (originalBm.getHeight() / 2));
bm3 = Bitmap.createBitmap(originalBm, 0, (originalBm.getHeight() / 2), (originalBm.getWidth() / 2), (originalBm.getHeight() / 2));
bm4 = Bitmap.createBitmap(originalBm, (originalBm.getWidth() / 2), (originalBm.getHeight() / 2), (originalBm.getWidth() / 2), (originalBm.getHeight() / 2));
}
//Make the background transparent
piece1.setBackgroundDrawable(new BitmapDrawable(getResources(), bm1));
piece1.setAlpha(0.2f);
piece2.setBackgroundDrawable(new BitmapDrawable(getResources(), bm2));
piece2.setAlpha(0.2f);
piece3.setBackgroundDrawable(new BitmapDrawable(getResources(), bm3));
piece3.setAlpha(0.2f);
piece4.setBackgroundDrawable(new BitmapDrawable(getResources(), bm4));
piece4.setAlpha(0.2f);
//Place parts in an array
parts.add(bm1);
parts.add(bm2);
parts.add(bm3);
parts.add(bm4);
//Shuffle the array
Collections.shuffle(parts);
//Assign the correct piece tag to each part
for(int i=0;i<4;i++){
if(i==1) {
part1.setImageBitmap(parts.get(i));
if (parts.get(i).equals(bm1)){
part1.setTag("piece1");
} else if (parts.get(i).equals(bm2)){
part1.setTag("piece2");
} else if (parts.get(i).equals(bm3)){
part1.setTag("piece3");
} else {
part1.setTag("piece4");
}
} else if(i==2){
part2.setImageBitmap(parts.get(i));
if (parts.get(i).equals(bm1)){
part2.setTag("piece1");
} else if (parts.get(i).equals(bm2)){
part2.setTag("piece2");
} else if (parts.get(i).equals(bm3)){
part2.setTag("piece3");
} else {
part2.setTag("piece4");
}
} else if(i==3){
part3.setImageBitmap(parts.get(i));
if (parts.get(i).equals(bm1)){
part3.setTag("piece1");
} else if (parts.get(i).equals(bm2)){
part3.setTag("piece2");
} else if (parts.get(i).equals(bm3)){
part3.setTag("piece3");
} else {
part3.setTag("piece4");
}
} else {
part4.setImageBitmap(parts.get(i));
if (parts.get(i).equals(bm1)){
part4.setTag("piece1");
} else if (parts.get(i).equals(bm2)){
part4.setTag("piece2");
} else if (parts.get(i).equals(bm3)){
part4.setTag("piece3");
} else {
part4.setTag("piece4");
}
}
}
}
private static int getImageId(Context context, String imageName) {
return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName());
}
private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
return true;
} else {
return false;
}
}
}
class MyDragListener implements View.OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
if(view.getTag().equals(v.getTag())){
if(view.getTag().equals("piece1")){
owner.removeView(view);
useMediaPlayer();
piece1.setBackgroundDrawable(new BitmapDrawable(getResources(), bm1));
piece1.setAlpha(0.9f);
corrects++;
} else if (view.getTag().equals("piece2")){
owner.removeView(view);
useMediaPlayer();
piece2.setBackgroundDrawable(new BitmapDrawable(getResources(), bm2));
piece2.setAlpha(0.9f);
corrects++;
} else if (view.getTag().equals("piece3")){
owner.removeView(view);
useMediaPlayer();
piece3.setBackgroundDrawable(new BitmapDrawable(getResources(), bm3));
piece3.setAlpha(0.9f);
corrects++;
} else if (view.getTag().equals("piece4")) {
owner.removeView(view);
useMediaPlayer();
piece4.setBackgroundDrawable(new BitmapDrawable(getResources(), bm4));
piece4.setAlpha(0.9f);
corrects++;
}
}
tries++;
if(corrects==4){
finish();
}
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
default:
break;
}
return true;
}
}
public void useMediaPlayer(){
mp.start();
}
public void againPuzzle(View v){
recreatePuzzle = true;
recreate();
}
public void finish(){
againButton.setVisibility(View.VISIBLE);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("originalBM", originalBm);
outState.putParcelable("bm1", bm1);
outState.putParcelable("bm2", bm2);
outState.putParcelable("bm3", bm3);
outState.putParcelable("bm4", bm4);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_games, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
return true;
case R.id.menu_about:
i = new Intent(this, About.class);
startActivity(i);
return true;
case R.id.menu_help:
i = new Intent(this, Help.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}

What about starting the same activity then finishing the previous one ? like :
Intent intent=new Intent(this, MainActivity.class)
startActivity(intent);
finish();
Using finish will help you not having any memoryleak as oncreate() do.
Second option : move the code selecting a puzzle in a new method (like selectNewPuzzle()), then call that method in onCreated and when you need select a new puzzle.
Third option : use a global boolean named like "canSaveInstance" wich is true when onCreate end. Encapsulate the lines saving the instancestate in an if statement checking this boolean, and when you need recreating, put this variable false, recreate as you usually do (so no data is saved, new puzzle is started) then reput it true (to handle config changes). You will need to be carefull when recreating activity : if(savedinstancestate!=null) must become "if(savedinstancestate!=null && canSaveInstance) (because if not you may try to load data which were not saved previously).
last option : (but i'm not really aware on how to do exactly) prevent user from launching multiple time the puzzle activity, then "launch" a new one (the older one will be overrided i think). You may need do some more research to do this one. As other options are easier I won't search for documentation helping doing that.

So i finally came up with a solution. What i was missing was to release the bitmap memory of my activity and that was what was causing memory leak. So, finally, me recreate() method looks like
public void againPuzzle(View v){
originalBm.recycle();
bm1.recycle();
bm2.recycle();
bm3.recycle();
bm4.recycle();
originalBm = null;
bm1 = null;
bm2 = null;
bm3 = null;
bm4 = null;
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
}

Related

How to get the text value from dropped text and update the scores when the dropped text is correct answer that stores in sqlite?

i have want to develop an word quiz gaming app and i was stuck in the drag and drop method. That is drag and drop option is working fine but my confusion is how to get the text view individually from another textview for to check the correct answer from my database sqlite
I have create a database with some questions and answers i want to update the score when the dropped text is the correct answer for the question..
Please help me...
`
public class QuizActivity1 extends Activity implements View.OnTouchListener, View.OnDragListener {
List<Question> quesList;
int score=0;
int qid=1;
Question currentQ;
TextView textView,textView9,textView8,textView2,textView3,textView4,textView5,textView6,textView7;
Button butNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
textView=(TextView)findViewById(R.id.textView);
textView2=(TextView)findViewById(R.id.textView2);
textView3=(TextView)findViewById(R.id.textView3);
textView4=(TextView)findViewById(R.id.textView4);
textView5=(TextView)findViewById(R.id.textView5);
textView6=(TextView)findViewById(R.id.textView6);
textView7=(TextView)findViewById(R.id.textView7);
textView8=(TextView)findViewById(R.id.textView8);
textView9=(TextView)findViewById(R.id.textView9);
textView.setOnTouchListener(this);
textView2.setOnTouchListener(this);
textView3.setOnTouchListener(this);
textView4.setOnTouchListener(this);
textView5.setOnDragListener(this);
textView6.setOnDragListener(this);
textView7.setOnDragListener(this);
textView8.setOnDragListener(this);
butNext=(Button)findViewById(R.id.butNext);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//if(qid<2){
currentQ=quesList.get(1);
setQuestionView();
// else{
//Intent in = new Intent("Quiz");
// startActivity(in);
// Toast.makeText(QuizActivity.this,"Failed",Toast.LENGTH_SHORT).show();
// recreate();
score +=4;
textView9.setText(String.valueOf(score));
Intent intent = new Intent(QuizActivity1.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
//}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.option, menu);
return true;
}
private void setQuestionView()
{
textView.setText(currentQ.getQuestion1());
textView2.setText(currentQ.getQuestion2());
textView3.setText(currentQ.getQuestion3());
textView4.setText(currentQ.getQuestion4());
qid++;
}
#Override
public boolean onDrag(View layoutview, DragEvent dragevent) {
int action = dragevent.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
View view = (View) dragevent.getLocalState();
TextView droptarget = (TextView) layoutview;
TextView dropped = (TextView) view;
view.setVisibility(View.VISIBLE);
if (droptarget.getText().toString() != dropped.getText().toString()) {
view.setVisibility(View.VISIBLE);
droptarget.setText(dropped.getText().toString());
//update the text in the target view to reflect the data being dropped
//make it bold to highlight the fact that an item has been dropped
//if an item has already been dropped here, there will be a tag
Object tag = droptarget.getTag();
//if there is already an item here, set it back visible in its original place
if (tag != null) {
//the tag is the view id already dropped here
int existingID = (Integer) tag;
//set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
//set the tag in the target view being dropped on - to the ID of the view being dropped
droptarget.setTag(dropped.getId());
butNext.setVisibility(View.VISIBLE);
//remove setOnDragListener by setting OnDragListener to null, so that no further drag & dropping on this TextView can be done
} else {
Toast.makeText(this, "false", Toast.LENGTH_SHORT).show();
}
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
default:
break;
}
return true;
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
view.setVisibility(View.VISIBLE);
return false;
}`enter code here`
}
}`

Android dynamic layout only shows after oncreate method finishes

isAnswered=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG,"1isanswered : "+isAnswered);
Log.d(TAG, "matchinggame OnCreate");
super.onCreate(savedInstanceState);
dbHandler = new DatabaseHandler(this);
rLayout = new RelativeLayout(this);
object = new ObjectObject();
imageAnswer=new ImageView(this);
setContentView(rLayout);
trainingID = getIntent().getExtras().getInt("trainingid");
trainingObjectList = new ArrayList<TrainingObject>();
trainingObjectList = dbHandler.getAllTrainingObject(trainingID);
//// TODO: 02.05.2016 egitim kismi oalcak burda tek tek gosterilecek
rLayout.setBackgroundColor(Color.MAGENTA);
// Handler handler = new Handler(); Log.d(TAG,"2isanswered : "+isAnswered);
for(int i=0;i<2;i++){Log.d(TAG,"i:"+i); Log.d(TAG,"3tisanswered : "+isAnswered);
final int finalI = i;
// handler.postDelayed(new Runnable() {
// #Override
// public void run() {Log.d(TAG,"thread i: "+finalI);
Log.d(TAG,"4isanswered : "+isAnswered);
isAnswered = 0;
TrainingObject trainingObject = new TrainingObject();
trainingObject = trainingObjectList.get(finalI);
objectCount = 2;
//test icin
Log.d(TAG,"testicin trainingobjectid: "+trainingObject.getTrainingobjectID());
object = dbHandler.getObjectObject(trainingObject.getTrainingobjectAnswer());
if(trainingObject.getTrainingobjectThree()!=0) objectCount++;
if(trainingObject.getTrainingobjectFour()!=0) objectCount++;
if(trainingObject.getTrainingobjectFive()!=0) objectCount++;
Log.d(TAG,"matchinggame objcount: "+objectCount);
RelativeLayout.LayoutParams rLayParams = new RelativeLayout.LayoutParams(140,140);
rLayParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
imgBytes = object.getObjectImageBlob();
bmp = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);
imageAnswer.setImageBitmap(bmp);
imageAnswer.setTag(trainingObject.getTrainingobjectAnswer());
imageAnswer.setId(R.id.imgAnswer);
rLayout.removeAllViews();
rLayout.addView(imageAnswer,rLayParams);
imageOne.setOnDragListener(MatchingGame.this);
imageAnswer.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) { Log.d(TAG,"6isanswered : "+isAnswered);
Log.d(TAG,"matchinggame setontouch");
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.d(TAG,"matchinggame setontouch if yes");
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE); Log.d(TAG,"7isanswered : "+isAnswered);
return true;
} else { Log.d(TAG,"8isanswered : "+isAnswered);
return false;
}
}
});
imageAnswer.setOnDragListener(MatchingGame.this);
Log.d(TAG,"*");
while(isAnswered==0){
//Log.d(TAG,"*");
}
// }
// }, 30000*finalI );Log.d(TAG,"thread sonrasi: "+finalI); Log.d(TAG,"5isanswered : "+isAnswered);
}Log.d(TAG,"ff");
}
because isanswered=0, it stays in infinite loop. But i cant see my layout, i only see magenta while this loop continues. After seconds black screen comes.
But when i disable while there, after printing ff to log, it finishes oncreate and then layout changes.
I searched but no answer for this.
Android layout only showing after oncreate method finishes
here it says try onstart. yes i tried but same.
i only have here asynctask call to get values from sqlite.
i have a for loop, in each iteration, i will show images and user will try to match them.
I did not use views or fragments.
Why doesnot it post to screen as soon as it takes view?
full activity is here:
https://gist.github.com/anonymous/87ccd4147ae0d202244bb78f51844f29
This happens because activity is not shown before onCreate method is finished. Try moving your code to onResume and see if it works. Also, in your current code, don't forget to call super.onCreate(savedInstanceState)

How to save/Restore state of Views of fragment on Tab change

I am following this tutorial.
there are 3 tabs in my App. in tab3 I m making changes to some views (like buttons and EditText spinners etc) and on the behalf of these changes i have to perform some actions in tab2. Simply you can say that i Change some values in tab3 and effect takes places in tab2. I know how to do this. I just want that my values of view becomes resets every time to default values when switching between the tab2 and tab3
My question is that how can i save the states of my views. so that on resuming the tabs i must get the default look of my views as i had left previously.
One thing more i tell you that i m doing all the work in onCreateView() methos. is this correct way. like this.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Toast.makeText(getActivity(), "onCreateView", Toast.LENGTH_SHORT)
.show();
if (container == null) {
return null;
}
View vi = inflater.inflate(R.layout.settings, container, false);
btnInsert = (Button) vi.findViewById(R.id.btnInsert);
btnInsert.setOnClickListener(this);
btnPosition = (Button) vi.findViewById(R.id.btnPosition);
btnPosition.setOnClickListener(this);
txtPosition = (TextView) vi.findViewById(R.id.txtPosition);
txtLogo = (TextView) vi.findViewById(R.id.txtLogo);
imgLogoPreview = (ImageView) vi.findViewById(R.id.imgLogoPreview);
imgLogoPreview.setOnClickListener(this);
edTxtUserText = (EditText) vi.findViewById(R.id.edTxtPreview);
relLogo = (RelativeLayout) vi.findViewById(R.id.RelLogo);
relText = (RelativeLayout) vi.findViewById(R.id.RelText);
logoWheel = (WheelView) vi.findViewById(R.id.wheelLogo);
logoWheel.setAdapter(new ArrayWheelAdapter<String>(logoWheelList));
logoWheel.setVisibleItems(4);
logoWheel.setCurrentItem(1);
positionWheel = (WheelView) vi.findViewById(R.id.wheelPosition);
positionWheel.setAdapter(new ArrayWheelAdapter<String>(
positionWheelTextList));
// LogoWheel changed listener
changedListenerLogo = new OnWheelChangedListener() {
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if (!wheelScrolled) {
}
}
};
logoWheel.addChangingListener(changedListenerLogo);
// Wheel scrolled listener
scrolledListenerLogo = new OnWheelScrollListener() {
public void onScrollStarts(WheelView wheel) {
wheelScrolled = true;
}
public void onScrollEnds(WheelView wheel) {
wheelScrolled = false;
btnInsert.setText(logoWheelList[wheel.getCurrentItem()] + "");
wheel.setVisibility(View.INVISIBLE);
if (wheel.getCurrentItem() == 2) {
txtPosition.setVisibility(View.INVISIBLE);
btnPosition.setVisibility(View.INVISIBLE);
relText.setVisibility(View.INVISIBLE);
relLogo.setVisibility(View.INVISIBLE);
} else if (wheel.getCurrentItem() == 1) {
relText.setVisibility(View.VISIBLE);
relLogo.setVisibility(View.INVISIBLE);
txtPosition.setVisibility(View.VISIBLE);
btnPosition.setVisibility(View.VISIBLE);
btnPosition.setText("Top");
positionWheel.setAdapter(new ArrayWheelAdapter<String>(
positionWheelTextList));
positionWheel.setVisibleItems(4);
positionWheel.setCurrentItem(1);
} else if (wheel.getCurrentItem() == 0) {
relLogo.setVisibility(View.VISIBLE);
relText.setVisibility(View.INVISIBLE);
txtPosition.setVisibility(View.VISIBLE);
btnPosition.setVisibility(View.VISIBLE);
btnPosition.setText("Top Left");
positionWheel.setAdapter(new ArrayWheelAdapter<String>(
positionWheelLogoList));
positionWheel.setVisibleItems(4);
positionWheel.setCurrentItem(1);
}
}
};
logoWheel.addScrollingListener(scrolledListenerLogo);
// /////////////////////Positon Wheel Listners///////////
// LogoWheel changed listener
changedListenerPosition = new OnWheelChangedListener() {
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if (!wheelScrolled) {
}
}
};
positionWheel.addChangingListener(changedListenerPosition);
// Wheel scrolled listener
scrolledListenerPosition = new OnWheelScrollListener() {
public void onScrollStarts(WheelView wheel) {
wheelScrolled = true;
}
public void onScrollEnds(WheelView wheel) {
wheelScrolled = false;
String btnStatus = btnInsert.getText().toString();
if (btnStatus.equals("Logo")) {
btnPosition.setText(positionWheelLogoList[positionWheel
.getCurrentItem()] + "");
} else if (btnStatus.equals("Text")) {
btnPosition.setText(positionWheelTextList[positionWheel
.getCurrentItem()] + "");
}
wheel.setVisibility(View.INVISIBLE);
}
};
positionWheel.addScrollingListener(scrolledListenerPosition);
return vi;
}
at what point i must save the states and at what point i should retrieve the savedstates?
Please tell me the how to implement the lifecycle of fragment in simple words.
i also tried the saveInstance() method of fragment. but not called.
Thanks
If I understand you correctly then this might be useful. Instead of recreating Fragments each time you can hide and show them.
This of course preserves your Fragments so is possibly only something you'd do it you had a few tabs. The advantage of this is that
You don't need to worry about saving data and recreating the fragment
Changes are available immediately to the user as soon as the relevant tab is selected.

Keep GridView when resuming from sleep mode

I got a problem with a quite large GridView.(about 70 children) The GridView works fine if I start it on onCreate or after resumeing after pressing the home button and then return. But when I resume after coming back from sleep mode, my BaseAdapter starts again and ruin the changes I have done to it during runtime. This also make getChildAt() give a NullPointerException if I am calling it just after restart.
How can I make it just do what regular onPause(home button) does to the GridView, and avoid that the GridView is wiped out everytime I am resumeing from sleep mode?
Edit:
I have tried setting a wakelock for my Activity class that calls the BaseAdpter with no luck
2.Edit: Since I posted this question I have played around with trying to restore the GridView using this code in onPause:
SparseArray<Parcelable> array = new SparseArray<Parcelable>();
gridView.saveHierarchyState(array);
bundle = new Bundle();
bundle.putSparseParcelableArray("state", array);
And this in onResume:
try{
gridView.restoreHierarchyState(bundle.getSparseParcelableArray("state"));
}
catch(Exception e){
//Most likely first start
Log.i("SomeTag", "No GridView state found");
}
}
The strange thing is everything I seems to have jumped from one place to another on the screen and it is still crashing when I try to getChildAt(). It is also failing to get it after sleep mode.
Edit Here is the code from BaseAdapter getView(Note! some of this code is irrelevant)
public View getView (int position, View convertView, ViewGroup parent) {
mParentView = parent;
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
//sets the height for every individual box
int box = width/7*6/10;
ImageCell v = null;
if (convertView == null) {
// If it's not recycled, create a new ImageCell.
v = new ImageCell (mContext);
v.setLayoutParams(new GridView.LayoutParams(box, box));
v.setScaleType(ImageView.ScaleType.CENTER_CROP);
v.setPadding(0, 0, 0, 0);
} else {
v = (ImageCell) convertView;
}
v.mCellNumber = position;
v.mGrid = (GridView) mParentView;
v.mEmpty = true;
int id = 200;
v.setId(++id);
String map = str[position];
int pos = position;
int up = pos-10;
int down = pos+10;
int left = pos-1;
int right = pos+1;
if(up>=0){
above = str[up];
}
else{
//Do nothing
}
if(down<70){
under = str[down];
}
else{
//Do nothing
}
if(left<=-1){
//Do nothing
}
else{
lefte=str[left];
}
if(right>=70){
//Do nothing
}
else{
righte=str[right];
}
//if(left>-1|left!=9|left!=19|left!=29|left!=39|left!=49|left!=59){
// lefte = str[left];
// }
// else{
// Log.i("ImageCellAdapter", "Left is trying to break walls "+left);
//Do nothing
// }
if (map.equals("mountain")) {
//Checks surroundings to find out witch drawable to set
v.setBackgroundResource(R.color.cell_empty);
v.mEmpty = false;
//All
if(above=="mountain"&&under=="mountain"&&lefte=="mountain"&&righte=="mountain"){
v.setImageResource(R.drawable.mountain_full);
}
//Single
else if(above=="mountain"&&under!="mountain"&&lefte!="mountain"&&righte!="mountain"){
v.setImageResource(R.drawable.mountain_down);
}
else if(above!="mountain"&&under=="mountain"&&lefte!="mountain"&&righte!="mountain"){
v.setImageResource(R.drawable.mountain_up);
}
else if(above!="mountain"&&under!="mountain"&&lefte!="mountain"&&righte=="mountain"){
v.setImageResource(R.drawable.mountain_left);
}
else if(above!="mountain"&&under!="mountain"&&lefte=="mountain"&&righte!="mountain"){
v.setImageResource(R.drawable.mountain_right);
}
//Double
else if(above=="mountain"&&under!="mountain"&&lefte!="mountain"&&righte=="mountain"){
v.setImageResource(R.drawable.mountain_left_down);
}
else if(above!="mountain"&&under=="mountain"&&lefte!="mountain"&&righte=="mountain"){
v.setImageResource(R.drawable.mountain_left_up);
}
else if(above=="mountain"&&under!="mountain"&&lefte=="mountain"&&righte!="mountain"){
v.setImageResource(R.drawable.mountain_right_down);
}
else if(above!="mountain"&&under=="mountain"&&lefte=="mountain"&&righte!="mountain"){
v.setImageResource(R.drawable.mountain_up_right);
}
else if(above!="mountain"&&under!="mountain"&&lefte=="mountain"&&righte=="mountain"){
v.setImageResource(R.drawable.mountain_up_down);
}
else if(above=="mountain"&&under=="mountain"&&lefte!="mountain"&&righte!="mountain"){
v.setImageResource(R.drawable.mountain_up_down);
}
//Triple
else if(above!="mountain"&&under=="mountain"&&lefte=="mountain"&&righte=="mountain"){
v.setImageResource(R.drawable.mountain_left_right_down);
}
else if(above=="mountain"&&under=="mountain"&&lefte=="mountain"&&righte!="mountain"){
v.setImageResource(R.drawable.mountain_left_up_down);
}
else if(above=="mountain"&&under!="mountain"&&lefte=="mountain"&&righte=="mountain"){
v.setImageResource(R.drawable.mountain_left_up_right);
}
else if(above=="mountain"&&under=="mountain"&&lefte!="mountain"&&righte=="mountain"){
v.setImageResource(R.drawable.mountain_up_right_down);
}
//None
else{
v.setImageResource(R.drawable.mountain);
}
}
else if(map=="start"){
List<String> posOf = Arrays.asList(str);
startPos=posOf.indexOf("start");
v.mEmpty=false;
v.setBackgroundResource(R.color.cell_empty);
getDur();
BitmapDrawable first = (BitmapDrawable)mContext.getResources().getDrawable(R.drawable.gress);
BitmapDrawable second =(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ic_launcher);
BitmapDrawable third = (BitmapDrawable)mContext.getResources().getDrawable(R.drawable.gress);
BitmapDrawable fourth = (BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ic_launcher);
final AnimationDrawable ani = new AnimationDrawable();
ani.addFrame(first, duration);
ani.addFrame(second, duration);
ani.addFrame(third, duration);
ani.addFrame(fourth, duration);
ani.setOneShot(true);
v.setImageDrawable(ani);
checkIfAnimationDone(ani);
v.post(new Runnable() {
public void run() {
ani.start();
}
});
}
else if(map=="stop"){
v.mEmpty=false;
v.setBackgroundResource(R.color.cell_empty);
v.setImageResource(R.drawable.ic_launcher);
v.setTag(1);
}
else if(map=="grass"){
v.mEmpty=false;
v.setBackgroundResource(R.drawable.gress);
}
else{
// v.setBackgroundResource (R.color.drop_target_enabled);
v.setBackgroundResource (R.color.cell_empty);
}
//v.mGrid.requestDisallowInterceptTouchEvent (true);
//v.setImageResource (R.drawable.hello);
// Set up to relay events to the activity.
// The activity decides which events trigger drag operations.
// Activities like the Android Launcher require a long click to get a drag operation started.
return v;
}
And defining the GridView in onCreate:
gridView= new BoxView(this);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
w.getDefaultDisplay().getSize(size);
Measuredwidth = size.x;
Measuredheight = size.y;
}else{
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();
}
int width = Measuredwidth/7*6;
gridView.setLayoutParams(new GridView.LayoutParams(width,LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL));
gridView.setNumColumns(columns);
gridView.setVerticalSpacing(0);
gridView.setHorizontalSpacing(0);
gridView.setPadding(0, 0, 0, 0);
gridView.setId(101);
gridView.setSelector(android.R.color.transparent);
gridView.setAdapter (new ImageCellAdapter(this, MAP));
I have noticed one thing in you code:
gridView.setLayoutParams(new GridView.LayoutParams(width,LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL));
Even if GridView is a ViewGroup, you can't access it's LayoutParams. Just think it trough, if this would be possible that means you could put a GridView inside another GridView.
Fix this before going further because is messing with you.
If you want your GridView to be inside a LinearLayout, for example, try this:
gridView.setLayoutParams(new LinearLayout.LayoutParams(width,LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL));
Here is the correct implementation of this answer:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*You have to implement what things from the gridView sould be "saved"*/
GridView gridView = new GridView(this) {
#Override
public Parcelable onSaveInstanceState() {
// Create the Parceable object with the things you want to save
Parceable stateOfGrid = ....etc
return stateOfGrid;
}
#Override
public void onRestoreInstanceState(Parcelable state) {
// Restore your grid's parameters that you previously implemented in onSaveInstanceState
super.onRestoreInstanceState(state);
...
}
};
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
Parcelable state = savedInstanceState.getParcelable("state");
if (state != null) {
gridView.onRestoreInstanceState(state);
Log.d(this.getClass().getName(), "state restored!");
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// Put your grid's Parceable into the bundle
super.onSaveInstanceState(outState);
Parcelable state = gridView.onSaveInstanceState();
outState.putParcelable("state", state);
}
}
Try using this tag:
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
You can use onSaveInstanceState (Bundle outState)
to save state of your gridview
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Parcelable state = gridView.onSaveInstanceState();
outState.putParcelable("state", state);
}
then in onCreate after you seted adapter to grdiview add this code
if (savedInstanceState != null) {
Parcelable state = savedInstanceState.getParcelable("state");
if (state != null) {
gridView.onRestoreInstanceState(state);
Log.d(this.getClass().getName(), "state restored!");
}
}
Is your application running in Landscape Mode?
if yes, then you should consider adding the tag
"android:configChanges="keyboard|keyboardHidden|orientation" for your activity in Manifest.xml, which will prevent android system from killing your activity and restarting it when you unlock the screen.

Keep a view alive while switching activity

I created a game, where I rotate through multiple activitys. Each activity stays just for a few seconds. Now I should add ads to the game. Since it doesn't make sense if the ad refreshes after just a few seconds I have to create a view which stays alive the whole time even if I start a new activity.
Since a view is bind to an activity (?) it might not be possible. So I wonder wether there is another solution to keep the adView alive while the content views are rotating.
Thanks in advance.
Edit:
Here is a simple Activity which is part of the activity cycle:
public class Punish extends ActivityWithSound implements OnClickListener {
#SuppressWarnings("unused")
private final String TAG = "Punish";
private RelativeLayout buttonContainer;
private ImageView bgImg;
private TextView nameTxt;
private TextView questTxt;
private Button mainMenuBtn;
private Button okBtn;
private Bundle bundle;
#Override
protected void onCreate(Bundle savedInstanceState) {
bundle = getIntent().getExtras();
if(bundle == null)
bundle = StbApp.getTempBundle();
setContentView(R.layout.quest);
setupView();
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
soundtrack.startFX(R.raw.fx_execution);
super.onResume();
}
#Override
protected void onPause() {
StbApp.getTempBundle().putInt(Victim.VICTIM, bundle.getInt(Victim.VICTIM));
soundtrack.stopAllFX();
super.onPause();
}
#Override
public void onClick(View view) {
if (view == mainMenuBtn){
//TODO Continue Function
Intent intent = new Intent(this, TitleScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
StbApp.setContinueBtn(true);
StbApp.getLastActivity().setClass(this, Punish.class);
startActivity(intent);
}
if (view == okBtn){
if (StbApp.getPenalty() == PenaltyType.LEAVE){
StbApp.getPlayer().remove(bundle.getInt(Victim.VICTIM));
StbApp.setNumberOfPlayer(StbApp.getNumberOfPlayer()-1);
}
if (StbApp.getNumberOfPlayer() == 2 && StbApp.getPenalty() == PenaltyType.LEAVE)
startActivity(new Intent(this, GameOver.class));
else {
startActivity(new Intent(this, Round.class));
}
}
}
#Override
public void onBackPressed() {
return;
}
private void setupView() {
float textSize = (float) getResources().getDimension(R.dimen.standard_text_size)/getResources().getDisplayMetrics().scaledDensity;
buttonContainer = (RelativeLayout) findViewById(R.id.container_button);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) findViewById(R.id.container_button).getLayoutParams();
params.setMargins(0, 0, 0, (int) (StbApp.AdHeight*1.3));
buttonContainer.setLayoutParams(params);
bgImg = (ImageView) findViewById(R.id.imgv_girl);
Log.d(TAG, "PlayerSize " + StbApp.getPlayer().size());
nameTxt = (TextView) findViewById(R.id.text_victim_name);
Log.d(TAG, "PlayerSize " + StbApp.getPlayer().size());
Log.d(TAG, "PlayerIndex bundle.getInt(Victim.VICTIM) " + bundle.getInt(Victim.VICTIM));
nameTxt.setText(StbApp.getPlayer().get(bundle.getInt(Victim.VICTIM)).getName());
nameTxt.setTextSize(textSize);
questTxt = (TextView) findViewById(R.id.text_quest);
switch (StbApp.getPenalty()) {
case LEAVE:
questTxt.setText(getResources().getString(R.string.punish_leave));
break;
case DRNK:
questTxt.setText(getResources().getString(R.string.punish_drink));
break;
case UNDRESS:
questTxt.setText(getResources().getString(R.string.punish_undress));
break;
}
questTxt.setTextSize(textSize);
mainMenuBtn = (Button) findViewById(R.id.button_mainmenu);
mainMenuBtn.setOnClickListener(this);
okBtn = (Button) findViewById(R.id.button_ok);
okBtn.setOnClickListener(this);
}
#Override
protected void onDestroy() {
if (bgImg.getDrawable() != null){
bgImg.getDrawable().setCallback(null);
bgImg.setImageDrawable(null);
}
super.onDestroy();
}
What I need would be an alternative for onDestroy, onPause and onResume.
A solution could be using a ViewFlipper/ViewSwitcher instead of jumping activities and then placing the adsView over or under the ViewFlipper/ViewSwitcher - It will however probably require quite a large re-write of your app.

Categories

Resources