How to uncall a method in android? - android

I am not sure if this is possible or not but what I am after is that, I have a method which I called in my onCreate and the method runs when the app is starts, basically this method does bunch of things like put numbers on Textview, change colour of text etc (this method does around 12 things atm). I have a button, what I want to do is, when the button is pressed I want to stop using the method that was called on start. For example;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMethod();
removeMethod();
}
public void removeMethod(){
Code for button and listener
.......
.......
.......
.......
{

you can do it with putting it in a thread, you can pause/stop as you wish.
here is simple tutorial on thread in java, you can put your in a thread which you want to do in setUpMethod().
http://www.tutorialspoint.com/java/java_multithreading.htm
and use Thread control methods to control the execution of code.
http://www.tutorialspoint.com/java/java_thread_control.htm

You just need to read through setUpMethod(), work out what the reverse of each action of it is, and put all those undo steps in removeMethod(). Post the code of setUpMethod() if you need more help.

If you do those things on main thread, you can't stop, because everything you do is on one threat. 12 things should finish and after this removeMethod() will be called.
Simply revert all those 12 changes to initial state in removeMethod method -set text, textColor and other things to initial values

To "stop the method being called again when the button is pressed", create a boolean somewhere, and add the line
if (wasRunAlready) return;
to the start and
wasRunAlready = true;
to the end of setUpMethod(). It will then be impossible for the code inside to run twice.

Related

Will the Activity's onResume() run forever?

In android, assuming that the first time I run my app, I want to have a block of code that runs (theoretically) forever assuming that the activity is always visible, should I just do the following:
public void onResume(){
// Block of code
}
or
public void onResume(){
while(true){
// Block of code
}
}
Another way of asking this would be:
In the first case, can I assume that once that block of code finishes running, that Android will re-run it again ? I tried looking for an answer for this but couldn't find any.
Thanks.
onResume runs when the activity is made visible,if you write and infinite loop inside on resume the the application will stop responding. If you have to run some code forever you should do it as a service. whatever you have to do an infinite loop is not the right way.
No, You have to choose your second method because in your first method your block of code will get called once when there is a state transition i.e. state changes from any state(like onPause) to onResume and your block of code executed once. But after that if there is no state transition then your code will not be called.
onResume() gets called whenever your activity is brought to the front. This code will be called only once. If you want to re-run your code, you need to use your second approach.

How to determine when the app is loading in Android

I have been trying to find guidance on how to make a loading splash screen in Android, and have found tutorials such like A Simple Android Splash Screen. Because this tutorials glosses over certain things, one thing that's a bit unclear to me is how do you programmatically determine whether the app is trying to load (before the onCreate() method for your activity executes and your activity loads)?
You might be interested in the Instrumentation Class. It is used to monitor such things, and should provide you with whatever you want know. Also, putting the Dalvik messages to Verbose in LogCat will give you an idea of everything the system is doing prior to calling your Activity's onCreate() method.
The onCreate() method of android is called when its time for it to render the UI. The condition that you are specifying by
one thing that's a bit unclear to me is how do you programmatically
determine whether the app is trying to load (before the onCreate
method for your activity executes and your activity loads)?
would be using someting in manifest like splashScreen="#drawable/Splash" but this unfortunately doesnt exist
So the solution would be like this
Calling a temporary XML file before loading your actual content
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
// Do your loading code here
// Create an AsyncTask if the task is time consuming
//Now Load your actual UI
setContentView(R.layout.activity_main);
}
you said
The issue is for my app while onCreate() is executing there is a black
screen for some time
The short black screen is for very few seconds and there is no way around for it.

How to call a method with View param

I have a source code of an app for android which get location from network or gps. There is the next code:
// Callback method for the "both providers" button.
public void useCoarseFineProviders(View v) {
mUseFine = false;
mUseBoth = true;
setup();
}
There is a button, and on the onClick event call to "useCoarseFineProviders", my question is that I want to delete this button and call this method from the onCreate method, but I don't know how to do this.
I need to learn so much things. Thanks for your help.
Your code isn't actually doing anything, or using the view parameter passed in. so if you want to move it to onCreate, just do it- take the body of the function, paste it into the bottom of onCreate, then delete this function and the code (probably in your xml) telling it to call this function in onClick.

What does "setContentView" really do?

In my main activity, I would like to have it set up, so that I first get met by a contentView just showing a background and some text. After X seconds, I want to change to my other view (GLSurfaceView).
This is obviously something I am doing completely wrong.
This is how I've imagined it could've been done (it's all in the onCreate method):
setContentView(R.layout.main);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
viewer = new Viewer(this);
setContentView(viewer);
Where layout Main is what I want to show at the beginning and Viewer is my GLSurfaceView class.
What happens is that it just goes black for 10 seconds and then it starts loading the objects I've got that is shown through OpenGLES.
There's nothing wrong with the layout Main, since it works if I just erase the lines under where the Thread.sleep takes action. Though, nothing happens before the Thread.sleep is over...
With that said, my questions are following:
Why is the contentView not changing until after Thread.sleep is done?
What would be an appropiate solution to what I want to achieve?
I'm assuming this in your onCreate() and thats why you are seeing nothing.
The way I would implement this is to start a thread using AsyncTask sleep in the doInBackground and in the onPostExecute set up the new view.
Don't make sleep the main thread(UI thread).Use a threads,AsynkTask or TimerTask for that type of works instead.
You're not sleeping the UI thread in the way you think you are.
The simplest thing for what you're looking to achieve is to separate the views into separate activities and let Android handle the transition between the views. It adds another file to your codebase, but it's fairly straightforward. Let's say your initial, plain view (R.layout.main) is for a SplashActivity activity, and your post-splash view goes into PostSplashActivity. Then you could do something like this:
public class SplashActivity extends Activity {
private static long DELAY = 10000; //milliseconds;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Handler().postDelayed(
new Runnable() {
#Override
public void run() {
Intent postSplash = new Intent(SplashActivity.this, PostSplashActivity.class);
SplashActivity.this.startActivity(postSplash);
SplashActivity.this.finish();
}
}, DELAY);
}
}
This will draw your R.layout.main layout, and then puts a startActivity call for your PostSplashActivity on the message queue and tells the queue to wait DELAY milliseconds to execute it.
It seems like you are making the main thread sleep. This may be why the code is running tell after.
It sounds like you want something like a splash screen. I like to think of these as separate to the following screen, so always use a separate activity rather than calling setContentView twice. You'd still need to sleep in a thread.
Just personal preference though...

ViewFlipper - how to force it to show the first View?

when I add a ViewFlipper, the UI thread seems to wait for the onCreate() method in the activity to be finished. Then it shows the second view. Why does it happen?
My current code is:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
viewFlipper = (ViewFlipper) findViewById(R.id.ScreenSwitch);
viewFlipper.setInAnimation(AnimationUtils.makeInAnimation(this, true));
viewFlipper.setOutAnimation(AnimationUtils.makeOutAnimation(this, true));
//do the necessary loading, when the splash screen persists
doSomeLoading();
viewFlipper.showNext();
}
Actually, the doSomeLoading consists of a for loop counting to ten millions and doing nothing. Now it just waits for loop to be done and shows the second view.
I would really appreciate a solution without having to create a separate Thread, because it seems to be pointless, invalidate() doesn't help there.
Maybe with viewFlipper.showPrevious();?
EDIT
I don't have your full code but here is the idea :
When you do long loading or slow actions, most of the time, you can think AsynTask or background thread.
So you need to create a new inner class. Lets say AsyncLoader. You will implement the method doInBackground() of this class and put your doSomeLoading() in it.
Now, implement onPostExecute() and put your viewFlipper.showNext(); in it.
Then, in your onCreate() method, replace the doSomeLoading() by new AsyncLoader.execute();
This should be nice. I might have forgotten some stuff as it's not real code. Check this for more explanations.

Categories

Resources