Is there any function to call after activity load on screen? - android

Is there any function to call after activity load on screen ? I need to show AlertDialog in some occasions and if I put that in onResume it looks strange, dialog is already visible and I want to user see when AlertDialog popup.

I think maybe you want onWindowFocusChanged(). You are going to want to add some extra logic to this method call though because it will be called anytime the window gains or loses focus. Not sure what your use-case is but could just add a global boolean to see if it was the first call or not.

you can go for this
place below code in onCreate() methode
Handler handler = new Handler();
handler.postDelayed(runnable, 5000);
and implement the runnable as
public Runnable runnable = new Runnable() {
public void run() {
}
};
in the postDelayed(runnable,decide your time) this will help you.
i hope i give you answer.

Related

Why do I need a delay before showing AlertDialog?

Under some conditions, when my app starts, it displays an AlertDialog. However, the alert never gets displayed. I discovered that if I add a delay, it works (i.e. gets displayed).
More specifically: on app startup, it executes the main activity onCreate() which under a certain condition starts a 2nd activity. In the 2nd activity, through a separate thread, it makes a check for some web server status. If the Android device doesn't have Internet connectivity, HttpURLConnection returns an error instantly and my enclosing function executes a callback to the 2nd activity. My code then uses post() to attempt to display an alert to the user (using post allows displaying the alert on the UI thread, which is required).
Apparently it tries to display the alert before any of the either activity's UI has been created. If I use postDelayed() in the 2nd activity, the problem still persists. However, if I use the following block of code in the main activity, the alert shows properly:
new Handler().postDelayed (new Runnable ()
{
#Override public void run()
{
Intent intent = new Intent (app, MyClass.class);
app.startActivityForResult (intent, requestCode);
}
}, 3000);
My solution is a hack that happens to work at the moment. I don't mind having a little delay on start-up for this particular situation but I don't want a delay that's longer than necessary or one that may sometimes fail.
What is the proper solution?
Ok, here's a workaround. First, I'll speculate that the problem is that the attempt to display the alert is happening before the looper for the UI thread has been started. Just a speculation.
To work around the problem I added a recursive post which gets called from onResume(), like this:
private boolean paused = true;
#Override public void onResume ()
{
super.onResume();
paused = false;
checkForAlert();
}
#Override public void onPause ()
{
super.onPause();
paused = true;
}
And here's the function that does the post:
private AlertInfo alertInfo = null;
private void checkForAlert()
{
if (alertInfo != null)
{
...code to build alert goes here...
alertInfo = null;
}
if (!paused)
contentView.postDelayed (new Runnable()
{
#Override public void run() { checkForAlert(); }
}, 200);
}
AlertInfo is a simple class where the thread needing the alert can put the relevant info, e.g. title, message.
So, how does this work? checkForAlert() gets called during onResume() and will continue to get called every 200ms until "paused" is false, which happens in onPause(). It's guaranteed to be recurring whenever the activity is displayed. The alert will get built and displayed if alertInfo is not null. In the secondary thread, I simply create an AlertInfo instance and then within 200ms the alert gets displayed. 200ms is short enough that most people won't notice the delay. It could be shorter but then battery use goes up.
Why did I start checkForAlert() in onResume instead of onCreate()? Simply because there's no need for it to run unless the activity is currently "on top". This also helps with battery life.

Wait for a View to redraw itself before the next line of code is executed

I have such a piece of code inside an onTouch listener:
game.setNextMove(move);
boardView.invalidate();
checkEnd();
computerMove(boardView);
I want to have the boardView redrawed before the checkEnd() method is done. How can i force it to do that?
I think you need to consider your design as mentioned in the comments, having said that you can achieve your goal by using Post() on a handler to execute checkEnd(). The reason this will work is that posting to the main handler will add it to the message queue which has already the call to Invalidate in it which will be executed before the call to Post() on the handler.
In short you can do:
boardView.invalidate();
Handler handler = new Handler()
hanlder.Post(new Runnable(
#Override
public void run () {
checkEnd();
}
));

Automatically start execution upon activity launch

I'm working on an app that synchronizes some graphic UI events with an audio track. Right now you need to press a button to set everything in motion, after onCreate exits. I'm trying to add functionality to make the audio/graphical interaction start 10 seconds after everything is laid out.
My first thought is, at the end of onCreate, to make the UI thread sleep for 10000 miliseconds using the solution here and then to call button.onClick(). That seems like really bad practice to me, though, and nothing came of trying it anyway. Is there a good way to implement this autostart feature?
Never ever put sleep/delay on UI-thread. Instead, use Handler and its postDelayed method to get it done inside onCreate, onStart or onResume of your Activity. For example:
#Override
protected void onResume() {
super.onResume();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//do whatever you want here
}
}, 10000L); //the runnable is executed on UI-thread after 10 seconds of delay
}
Handler handler=new Handler();
Runnable notification = new Runnable()
{
#Override
public void run()
{
//post your code............
}
};
handler.postDelayed(notification,10000);
Yes, putting the UI thread to sleep isnt a good idea.
Try this
private final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
worker.schedule(task, 10, TimeUnit.SECONDS);

Control underlying elements after popping up a dialog

I am popping up a dialog from an Application context by calling an intent which has a transparent theme.(Similar to what is mentioned here.)
The pop is a read only.The issue is that when the pop up is shown there is a current activity going on in the background. I need to be able to access the background activity too while the popup is dispalyed. I have added the following two lines but it still is not possible for me to control the underlying activity:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
What am I doing wrong?
you must open the dialog in a thread, you haven't explained much about what you want to do in background ,but for making a thread do this:
create a new class with name:thread1 then write down these there:
public class thread1 extends Thread {
MainActivity activity;
public thread1(MainActivity m){
activity=m;
ProgressDialog pd;
//other code maybe for specification
pd=ProgressDialog.show(activity,"", "wait for background activity");
}
#Override
public void run(){
//you code working in background here like:
activity.sucess=0;
while (activity.sucess==0){
android.os.SystemClock.sleep(300);
}
pd.dismiss();
}
}
whenever you want to open this pb you should write this,
thread1 t1=new thread1(this);
t1.start();
and when ever your work is done and you want to close the pb, you must set sucess=1 then the thread will be killed.

How can I refresh the activity?

I want to refresh the activity as i want thatwithout firing any event some work gets performed and activity calls by itself. So, i want to know is there any option in android to refresh the activity by itself.
You can do this by yourself through a Handler on which you call postDelayed(..)
http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)
Put this in your class:
private final Handler handler = new Handler();
make a function called: doTheAutoRefresh() that does:
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
doRefreshingStuff(); // this is where you put your refresh code
doTheAutoRefresh();
}
}, 1000);
}
Call this function in your onCreate.
NOTE: this is the basic approach. consider stopping this after onPause has been called and to resume it after onResume. Look at the handler class to see how to remove.
You can create a thread and and call the refresh() with the task you want to refresh
for other questions I've pulled the most effective ways to do this are:
finish();startActivity(getIntent());
OR
// Refresh main activity upon close of dialog box
Intent refresh =new Intent(this, ActivityWeAreIn.class);
startActivity(refresh);
Note: this also works with Activity objects or from Fragments using getActivity()

Categories

Resources