Reputation: 2268
I have an activity with very basic Layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@+id/WebServiceClassInformation"
android:text = "Making connection with servers"
android:layout_centerInParent = "true"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content">
</TextView>
</RelativeLayout>
In my OnCreate()
method, I am updating the values of this text view. Its code is following:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.CallWebServiceLayout);
webServiceClassInfo = FindViewById<TextView> (Resource.Id.WebServiceClassInformation);
if (InternetConnectionCheck._InternetIsAvailable (this))
{
webServiceClassInfo.Text = "Stay Connected to Internet";
System.Threading.Thread.Sleep (1000);
}
StartService (new Intent(this, typeof(MyService)));
StartActivity (new Intent(this, typeof(Login)));
}
Now, here is an interesting thing. While debugging, when control is on "Stay Connected" line, and passes it to go to Sleep line, TextView does not show on my phone's screen and the updated text is also not shown, but it sleeps for 1 sec, and navigates to Login activity.
From there, when I press back, I can see the TextView with its updated value. Here is a screenshot from phone.
So this is the problem. I want user to see the text for 1 sec, but it navigates to next activity. Any suggestions ?? What am I missing here ?
Upvotes: 0
Views: 675
Reputation: 5234
You should override OnResume and put the code there. Make the resum async function and then use await. This will allow the UI to update itself. I have not tested the sample below but that should do the job.
protected async override void OnResume()
{
base.OnResume();
if (InternetConnectionCheck._InternetIsAvailable (this))
{
webServiceClassInfo = FindViewById<TextView> (Resource.Id.WebServiceClassInformation);
webServiceClassInfo.Text = "Stay Connected to Internet";
await Task.Delay(1000);
}
StartService (new Intent(this, typeof(MyService)));
StartActivity (new Intent(this, typeof(Login)));
}
Upvotes: 1
Reputation: 1597
Use postDelayed(Runnable r, long delayMillis) to start your activity and service.
Runnable r = new Runnable() {
@Override
public void run() {
StartService (new Intent(this, typeof(MyService)));
StartActivity (new Intent(this, typeof(Login)));
}
};
new Handler().postDelayed(r, 1000);
When u use Thread.sleep(1000) then it is normal that nothing happens for 1 second...
Upvotes: 0