I have an intent that when the user touches anywhere on the splashscreen activity they're directed to the gallery activity, this seemingly works fine for the most part and when running the app does as is expected until the back button is pressed. When pressed multiple presses are required to return to the splashscreen.
Using logcat I was able to find out that the intent is being run multiple times however I'm unable to understand why, here's the method for the intent.
private void FullScreenOnTouchEvent() {
LinearLayout layout = (LinearLayout) findViewById(R.id.activity_splashscreen_layout);
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
startActivity(new Intent(Splashscreen.this, Gallery.class));
Log.d("Splashscreen", "splashscreen executed");
return true;
}
});
}
I placed a check when the intent was executed and the new activity (gallery) had opened, the messages displayed are "splashscreen executed" and "gallery executed" respectively.
Here's a copy of the logcat.
splashscreen executed /
Gallery has executed /
splashscreen executed /
Gallery has executed /
I'm unable to see why this is ocurring and i'm at a bit of a deadend research wise, any help would be much appreciated.
Thank you,
Damon.
onTouch() is being called multiple times since it recognizes touch inputs of many kinds (DOWN, UP, MOVE...). You can fix this by an example written HERE or change layout listener to the OnClickListener which would be more simple to implement (less code).
Related
I have 2 screens corresponding to 2 activities on my Android app. I am trying to go back to the first activity from the second screen by clicking a Back button. Even though my first activity restarts, I get an annoying pop-up message "Unfortunately the app has stopped" as my app refreshes to go back to the first activity. I would like to not have this message appear.
I have tried different variations of code and the message appears each time :
#Override
public void onBackPressed() {
startActivity(new Intent(this, MainActivity.class));
}
or
public void finishActivity(DisplayMessageActivity v){
Intent intent = new Intent( this, MainActivity.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
or even just
public void finishActivity(DisplayMessageActivity v){
finish();
}
You should read the logs to find out what the exception is and why it is occurring. Places to check:
If back navigation logic is inside an OnClickListener, something inside the OnCLickListener code is probably null.
The activity it should go to after pressing back is having a null value for something you're trying to use.
Any asynchronous calls from old activity finishing after it has been removed.
Your log should tell you exactly what it is.
Many times in android apps, users click a button/view and start a new activity, where the new activity pops up in front of what the user was previously looking at, and loads data.
Would there be any difference from a user perspective if the data started loading (from network or disk or both) when the user clicked the button before the next activity started. And then that data was returned to the new activity in a broadcast receiver.
This is compared to starting the process in the oncreate of the activity. Assuming these network and i/o processes only take milliseconds either way, would it make a difference to the user if the methods were started in onCreate of the new activity, or started in the old activity onClick.
First way, starting I/O and changing views after I/O finishes
//first activity
public void onClick(View v){
startActivity(new Intent(this, NewActivity.class);
}
//NewActivity.class
onCreate(Bundle mBundle){
super.onCreate(mBundle);
setContentView(R.layout.mView);
mObject = networkCall(); //after network call, the view objects in this layout will reflect data from the network call
}
second way, starting the I/O in the first activity
//first activity
public void onClick(View v){
IntentService networkCall = new IntentService();
//start network call
startActivity(new Intent(this, NewActivity.class);
}
//second activity on create just sets the view and also broadcast receiver
My GUESS is that in the split second that it takes for the activity to pop up, the data from the intent service could become available. But at the same time, passing data via intent could take just as long making the benefits marginal
Insight appreciated
In my experience the onCreate() of your new activity is called almost instantly from when you call startActivity(). The new activity doesn't show up right way because it has to take time to render your layout.
You could play around with timings yourself by using the Log.d() function. Something like Log.d(TAG, "This happend at: " + System.currentTimeMillis()); at different points in your code to see when things happen. Watch the LogCat while your apps runs and you can decide for your self which way is better.
Is there a way to view all intents that are generated by the Android OS's at any moment but maybe filtered by the activity ? Specifically I am testing the onHoverListener - I want to know if my activity is throwing away the hover motionevent or whether none is being generated (system not capable/ some other problem)
Ideally I would like a log of all intents given to my activity - but some other trick is also fine.
Android intents are , capable for starting a new activity , service , complete any action i.e send email , click photo , fetch data from a content provider etc .
Capturing any intent of that kind , you need to have intent filters registered to your activity with the same actions , as that of system intents .[The framework will pick your actvity/app if the same intent is fired and hence you may be able to intercept those intents if the user prefers to] This may be little too much as there will be so many of the actions declared for different android components .But some popular examples are sending sms , picking a contact , send mail .
Please refer to this for more info :http://developer.android.com/guide/components/intents-filters.html
Events and Intents are different all together , all events generated by your app will go the event handler queue of the UI thread .
So in order to intercept you should set appropriate event listeners to your activity components .
Try using onTouch instead .
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Button Pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//finger was lifted
}
return false;
}`
I am trying to make a simple project that could move the application to background by using
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
moveTaskToBack(true);
}
return super.onKeyDown(keyCode, event);
}
and now I am trying to bring the application to the front, displaying on the screen automatically even though user didn't reopen the application
For example, I set the timeout as 30 seconds, then I close the application (which is moved to background actually), after 30 seconds, the application will automatically move to front and shows text "Time Out". If I am playing games during time out, the game will pause and display the Time Out page, I tried to search for the solutions but the result is quite disappointing. the below are the codes I using now to bring the page to front, but it is only display in the app, unless user reopen the app then onli they can see the Time Out page otherwise they will not know
Intent intent = new Intent("com.lolcash.lol.PopOut");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
is there any other way to do that?
Try to create a service, this will run in background. When you want to bring the activity to front you can start it from the Service.
Another solution would be to display a notification with a message, when the user will tap it you can take him to the app.
i want to close my whole application. i have tried out finishActivity() on back press but it doesn't work. I want to close my application on back press event completely. How could i do it?
public void onImageViewClicked(View view){
switch(view.getId()){
case R.id.viewStk:
intent = new Intent(MainActivity.this,ViewStkActivity.class);
startActivityForResult(intent, 12);
break;
case R.id.about:
Intent aboutIntent = new Intent(MainActivity.this,AboutActivity.class);
startActivity(aboutIntent);
break;
}
}
I use:
#Override
public void onDestroy()
{
super.onDestroy();
// Force the process to be killed on finish(), not kept around
System.runFinalizersOnExit(true);
System.exit(0);
}
which is called after using this.finish()
(This is an overridden method of the activity)
People will complain and tell you it breaks the Android app lifecycle, but having an "exit" button on my app during development was an immediate boost of my productivity.
Edit
Apparently, the latest version of the API notes that the runFinalizersOnExit function is deprecated and unsafe. I've had no issue with it on Gingerbread, ICS, or JB. But in interest of full disclosure, I'll point this out.
public void onClick(View v) {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
This will also help
Intent i = new Intent(yourScreen.this,Home.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", true);
startActivity(i);
and in the onCreate of the Home class, do this to check,
if (getIntent().getBooleanExtra("EXIT", false))
{
finish();
}
what this will essentially do is no matter at what activity you are, you can call the home screen with the clear top flag. In the home screen there is a check condition in the onCreate method which will help to clear the stack..Now ,if you press back button you will exit the app as the stack is cleared..
Let me know if the problem still persists...
In the activity.java-file that needs the implementation (fx MainActivity.java) insert this:
public void exitProgram(View view){
finish();
System.exit(0);
}
Just remember to also call the function in your .xml file, fx:
<Button
...
...
android:onClick="exitProgram"
...
/>
and finally in the buttons attributes, at the "text" attribute - press the small button next to the field and choose the one for the button you want to work with - done.
A good way to do it? No. Not at all. But if you just want a "panic-lets-get-outta-here"-button, then here you go.
There are a ton of different ways to implement the exit-functionality (one of them shown here) - as there are a ton of ways to implement the simple "Hello World" program - each has their value and you should really consider, why you have chosen the one you have.