8 Replies - 513 Views - Last Post: 16 June 2013 - 04:29 PM

#1 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

recreate() inside of a thread?

Posted 15 June 2013 - 10:16 PM

I am trying to use the recreate() function within a thread but it isn't working. There is no error message or crash, it just doesn't do anything. I know it must have somthing to do with it being inside of the thread because if I put recreate() inside of a button click it works just fine.

Any idea what is going wrong?

Starting_Screen.this.runOnUiThread(new Runnable() {
     public void run() {							
	    recreate();
	}		
     });


This post has been edited by mouthwash: 15 June 2013 - 10:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: recreate() inside of a thread?

#2 bucano  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 18
  • Joined: 13-May 13

Re: recreate() inside of a thread?

Posted 16 June 2013 - 03:24 AM

I'm just having a shot in the dark here. I'm not great at threading, the recreate method according to the Android API docs "Cause this Activity to be recreated with a new instance." however you're trying to call it from another thread to which the activity is on.

Have you tried actually trying to create a new instance of the class in the runnable E.g.
Starting_Screen.this.runOnUiThread(new Runnable() {
public void run() {
MyClass myClass = new MyClass();


Was This Post Helpful? 0
  • +
  • -

#3 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: recreate() inside of a thread?

Posted 16 June 2013 - 12:52 PM

I attempted that with not luck. I also tried to put it into a handler like this, which also didnt work:

handler = new Handler(Looper.getMainLooper());
in onCreate

and this:

handler.post(new Runnable() {
			    @Override
			    public void run() {
			        Intent intent = new Intent (Starting_Screen.this, Starting_Screen.class);
			        startActivity(intent);
			    }
			});	


This post has been edited by mouthwash: 16 June 2013 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: recreate() inside of a thread?

Posted 16 June 2013 - 01:02 PM

What is your motivation for needing a recreate()? Perhaps there is a better way to do this?
Was This Post Helpful? 0
  • +
  • -

#5 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: recreate() inside of a thread?

Posted 16 June 2013 - 01:04 PM

basically the player beats the level, I set a variable for the level as the next map/level, and I want to recreate the screen into that new level.
I need to recreate because the class needs to reset all the variables and recreate all the tiles... ect

This post has been edited by mouthwash: 16 June 2013 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: recreate() inside of a thread?

Posted 16 June 2013 - 01:12 PM

Ah. So there is a much easier way to do this. Make the level a state machine that implements several methods:

load(),
update(),
render(),
handleInput()

and then make each level be an instance of the State.

Here's an example in raw Java of how to do this:
http://www.dreaminco...21&#comment5192
Was This Post Helpful? 1
  • +
  • -

#7 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: recreate() inside of a thread?

Posted 16 June 2013 - 01:15 PM

why is that a better way then just using recreate()?
Just curious.
Also, why doesnt the recreate work?
I also need to be able to go to another class entirely from within the thread eg:
 Intent intent = new Intent (Starting_Screen.this, Menu.class);
	    startActivity(intent); 


So still need to fix the issue i think

This post has been edited by mouthwash: 16 June 2013 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: recreate() inside of a thread?

Posted 16 June 2013 - 03:00 PM

Perhaps it's a matter of the focus. In mind, it's simply about focus. It also depends on the game. Is it graphics or just GUI stuff?

In terms of your other question, you would normally push the startActivity method onto the GUI thread the way you did in post 1, but the issue is that you are doing that from another thread within itself. So honsestly, I'm not quite sure what's going on. Perhaps you could post the rest of the Activity and we'll see what we can do.
Was This Post Helpful? 0
  • +
  • -

#9 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: recreate() inside of a thread?

Posted 16 June 2013 - 04:29 PM

Here is the bulk of my code. The rest are just functions, which I didn't include because it will get too long and I dont think it really relates to the problem.
package com.example.triangles;




import java.util.ArrayList;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;

import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.onclickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import android.widget.TextView;

public class Starting_Screen extends Activity {
	//Declaring some global variables
	FrameLayout layoutMain;
	Context context;
	WakeLock wL;//For setting if the display can sleep or not.
	Handler handler;
	
	float defdisplayX=800;
	float defdisplayY=480;
	double resolxscale;
	double resolyscale;
	Display display;
	Point screenSize = new Point();
	
	Button bClear,bUndo,bMainMenu;
	TextView tvScore,tvCoins,tvCirclesLeft;
	ImageView doorBottom, doorTop;
	int doorBottomY, doorTopY;
	Drawable UnselectedImage,SelectedImage,TriangledImage;
	
	boolean bolSelected1 = false,bolSelected2 = false, bolSelected3 = false,bolSelected4 = false,winconditioncircles = false,winconditionarea = false,playerWon = false;
	public int circlesLeft,totalCircles,lineIndex = 0,PlayerScore =0,coins,MapName,minArea;
	float pos1x,pos1y,pos2x,pos2y,pos3x,pos3y,pos4x,pos4y;//positions of previously created triangle
	ImageButton[] Circles;
	Line[] Lines;
	
	Bitmap bitmapUnselected;
	Drawable drawableUnselected;
	StateListDrawable states;
	
	ArrayList<TextView> Area = new ArrayList<TextView>();
	ArrayList<View> triangleCircles = new ArrayList<View>();
	

	//Constructor Event
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		PowerManager pM = (PowerManager)getSystemService(Context.POWER_SERVICE);
		wL = pM.newWakeLock(PowerManager.FULL_WAKE_LOCK, "WakeLockHere");
		super.onCreate(savedInstanceState);
		wL.acquire();
		handler = new Handler(Looper.getMainLooper());
		requestWindowFeature(window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_starting__screen);	
		display = getWindowManager().getDefaultDisplay();
		display.getSize(screenSize);
		resolyscale = screenSize.y / defdisplayY;
		resolxscale = screenSize.x / defdisplayX;
		initialize(); // initalizes variables
		buttonclick(); // checks all button clicks
		CreateCircles();//create circles on the screen
		RunGame();//game thread
	}
	
	@Override
	protected void onstop() {
		// TODO Auto-generated method stub
		super.onstop();
		SharedPreferences prefs = getSharedPreferences("myPrefs",0);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt("myCoins", coins);
		editor.commit();
		wL.release();

	}

	@Override
	protected void onpause() {
		// TODO Auto-generated method stub
		super.onpause();
		wL.release();
	}

	@Override
	protected void onresume() {
		// TODO Auto-generated method stub
		super.onresume();
		wL.acquire();
	}
	public void restart(){
	    Intent intent = new Intent (Starting_Screen.this, Menu.class);
	    startActivity(intent);    
	}
	
	private void RunGame() {
		new Thread(new Runnable() {
			public void run() {
				while (true) {					
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						if (winconditioncircles == true){
							if (doorTopY > (-240*resolyscale)){
								doorTopY -= 5;
								doorBottomY +=5;
							}else if (doorTopY <= (-230*resolyscale)){
                                                                playerWon = true;
								
							}
						}
						DrawScreen();
					}
				}
			}
		}).start();
	}
    
	public void onRestart() {
    	super.onRestart();
    	Intent intent = getIntent();
    	finish();
    	startActivity(intent); 
     }
	
    private void DrawScreen() {
		Starting_Screen.this.runOnUiThread(new Runnable() {
			public void run() {				
				doorBottom.setY(doorBottomY);
				doorTop.setY(doorTopY);	
                                if (playerWon){
                                    restart();
                                }	
			}
		});
	}



}



This post has been edited by mouthwash: 16 June 2013 - 04:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1