I am trying to find a way to skip a flash screen by clicking on the screen of the activity. This is what I came to and it works. The problem is that after I click and the new activity is called, the boolean false default if runs again and the intent is called twice. What am I doing wrong?
RelativeLayout OnClickSkipScreen = (RelativeLayout)findViewById(R.id.SplashScreenView);
OnClickSkipScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OnClickSkip = true;
/*Loading.interrupt();
Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
startActivity(SplashScreen);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
finish();*/
}
});
Thread Loading = new Thread() {
#Override
public void run() {
if (!OnClickSkip) {
try {
sleep(2573);
Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
startActivity(SplashScreen);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
} catch (Exception e) {
e.printStackTrace();
} finally {
finish();
}
} else if (OnClickSkip) {
try {
Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
startActivity(SplashScreen);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
} catch (Exception e) {
e.printStackTrace();
} finally {
finish();
}
}
}
};
Loading.start();
}
use SharedPreference. Then think is whenever your class loads up it loads your boolean to its initial status which is false. so you will only meet one condition,all the time, which is the condition you are meeting now.
When i say persist, i mean you need to save your boolean state so as you can load it up from storage and check,with this your boolean will not be dependent on the initial state which is always false
so in your oncreate it could be this
SharedPreference sp = getPreference(0);
boolean OnClickSkip = sp.getBoolean("onclick",false);
now you can continue; if you want to save your boolean state
Editor e = sp.edit();
e.putBoolean("onclick",onClickSkip);
e.commit();
now continue to your codes
Related
I have a problem with my app. I have created a button called "browse". By clicking it, the button performs a google search and the app is no longer visible. Additionally, I have a countdowntimer. Now I want that the countdowntimer pauses as long as the app is not visible.
Do you have any ideas?
I have already tried the methods onPause(); and onResume(); but its not properly working.
browse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String escapedQuery = null;
resume = true;
countDownTimer.cancel(); //I have no idea where to resume
try {
escapedQuery = URLEncoder.encode(begriff2, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Uri uri = Uri.parse("http://www.google.com/#q=" + escapedQuery);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
You can cancel it ,in onPause() , like this :
#Override
public void onPause() {
countDownTimer.cancel();
super.onPause();
}
I'm facing with an intermittent application flow issue.
Here, I've got a Login screen where I'm able to login for the first time. But when I'm logging out and/or re-loging in, I'm unable to traverse further. As a work around, I need to uninstall my application and reinstall it and the flow is ok.
Can anyone please guide me on the possible issue.
LoginActivity.java
GetWebServiceManager passwordExpiryWSManager = new GetWebServiceManager();
//to check network connectivity (data connection/Wi-Fi)
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInformation = connectionManager.getActiveNetworkInfo();
if (networkInformation != null && networkInformation.isConnected() == true) {
try {
// passwordExpiryString = passwordExpiryWSManager.execute("http://172.25.164.143:8088/api/Login/GetExpiredPassword/?lContactKey=" + username.getText().toString()).get();
passwordExpiryString = passwordExpiryWSManager.execute(AppConstants.URL + "/Login/GetExpiredPassword/?lContactKey=" + username.getText().toString()).get();
jsonObjectPassExpiry = new JSONObject(passwordExpiryString);
jsonArrayPassExpiry = jsonObjectPassExpiry.getJSONArray("LstLoginDetail");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* Converting JSON string response from Webservice into Java Object
* */
if (jsonArrayPassExpiry != null) {
Gson gson = new Gson();
LoginTO[] loginExpTO = gson.fromJson(jsonArrayPassExpiry.toString(), LoginTO[].class);
loginListPassExpiry = Arrays.asList(loginExpTO);
intent = getIntent();
if (loginListPassExpiry.get(0).getlSuccess() == 1) {
intent.setClass(getApplicationContext(), PortfolioSummaryFragmentActivity.class);
// intent.putExtra("sToken", loginList.get(0).getsToken());
startActivity(intent);
} else {
intent.setClass(getApplicationContext(), PasswordExpiryActivity.class);
// intent.putExtra("sToken", loginList.get(0).getsToken());
startActivity(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Service is not responding. Please try again later!!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Network connection unavailable. Please check your data plan or Wi-Fi connection!!", Toast.LENGTH_SHORT).show();
}
Logout.java
public class LogoutActivity extends Activity {
private TextView successResponse;
private Intent intent;
private Button btnLLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logout_success);
successResponse = (TextView) findViewById(R.id.txtvwLSuccessMessage);
btnLLogin = (Button) findViewById(R.id.btnLLogin);
intent = getIntent();
successResponse.setText(intent.getStringExtra("successResponse"));
}
public void Login(View v) {
btnLLogin.setTextColor(getResources().getColor(R.color.black));
intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
#Override
public void onBackPressed() {
finish();
}
}
please use intent = new Intent(LogoutActivity.this, LoginActivity.class); instead of intent = new Intent(this, LoginActivity.class);
beside this use
most important
Add android:launchMode="singleTask " to the activity element in your manifest for Activity LoginActivity
I have made 4 activities in android they all are same. They just have a relative layout and that layout has 4 different images as backgroud,i have set animation on that for 2000miliseconds for example 1st screen should come from right.second from left...etc..I have implemented as below but its not working pls help me..!
screen1.java
Thread splashThread = new Thread() {
public void run() {
try {
sleep(2000);
} catch (Exception e) {
}
startAnimatedActivity(new Intent(SplashActivity1.this,
SplashActivity2.class),
CustAnimatedActivity.SLIDE_FROM_RIGHT);
finish();
}
};
splashThread.start();
same code for 3 activity also..!
i have used "handler" in place of "thread" .I have tried following code..and its working like butter..!
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
handler.sendEmptyMessage(1);
}
}, 2000);
}
private Handler handler = new Handler()
{
#SuppressWarnings("deprecation")
#Override
public void handleMessage(android.os.Message msg)
{
try
{
Intent intent = null;
intent = new Intent(SplashActivity1.this,
SplashActivity2.class);
startActivity(intent);
overridePendingTransition(R.anim.animated_activity_slide_left_in, R.anim.animated_activity_slide_right_out);
finish();
} catch (Exception e) {
}
}
};
I have an ActivityInstrumentationTestCase2 with a test executing Button.click(). The Button should start an other Activity to do some work.
I think Button.performClick() is performed correctly, but the test is finishing before the other Activity is executed.
#UiThreadTest
public void test() {
Intent i = new Intent(this.myActivity, MyActivity.class);
myActivity.startActivity(i);
Button button = (Button) myActivity.findViewById(R.id.button);
button.performClick();
}
I tried the following which worked but i think this is rather a work-around than a good solution.
public void test() {
Intent i = new Intent(this.myActivity, MyActivity.class);
myActivity.startActivity(i);
Button button = (Button) myActivity.findViewById(R.id.button);
button.performClick();
try {
Thread.sleep(50000);
} catch (InterruptedException e) {
Log.e("MyTest", e.getMessage());
}
}
IsnĀ“t there a better way?
This is my final solution:
public void test() {
Instrumentation instrumentation = getInstrumentation();
// Prepare a monitor for your activity
Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(MyActivity.class.getName(), null, false);
// Start your activity manually
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(instrumentation.getTargetContext(), MyActivity.class.getName());
instrumentation.startActivitySync(intent);
Activity myActivity = getInstrumentation().waitForMonitor(monitor);
Button upSend = (Button) myActivity.findViewById(R.id.button);
upSend.performClick();
Log.d("MyTest", "button clicked");
//wait for SecondActivity to start (called by MyActivity)
monitor = instrumentation.addMonitor(SecondActivity.class.getName(), null, false);
Activity secondActivity = getInstrumentation().waitForMonitor(monitor);
int count = 0;
//wait until SecondActivity is finishing
while(!secondActivity.isFinishing()) {
Log.d("MyTest", "waiting - " + ++count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e("MyTest",e.getMessage());
}
}
}
Thx again to Erik; now the second activity gets started by button and test is waiting for it to finish.
I have created a splash event and have it set to sleep for 3secs. Everything works fine but when you go to exit the application it takes you back to the splash. Is there a way to kill this with the code that I have or do I need to write this in a different manner.
public class splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openApp = new Intent("com.iqmobile.chris.IQMOBILEACTIVITY");
startActivity(openApp);
}
}
};
timer.start();
}
}
splash.this.finish(); after you start startActivity(openApp);
Intent openApp = new Intent("com.iqmobile.chris.IQMOBILEACTIVITY");
startActivity(openApp);
splash.this.finish();
Second Solution
in AndroidManifest file
<activity android:noHistory="true"
android:name=".splash" />
add finsh(); in code as :
Intent openApp = new Intent("com.iqmobile.chris.IQMOBILEACTIVITY");
startActivity(openApp);
splash.this.finsh();
mSplashThread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
wait(3000);
}
} catch (InterruptedException ex) {
}
finish();
Intent intent = new Intent();
intent.setClass(FirstActivity.this,
SecondActivity.class);
startActivity(intent);
}
};
mSplashThread.start();
}