Finished activity automatically reopens - android

I have a problem in a very simple application.
I have a main activity, and on button click I open a second activity:
newEntryButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Intent intent = new Intent(MainActivity.this, NewSpendingActivity.class);
MainActivity.this.startActivity(intent);
return true;
}
});
When I close this activity (for example by tapping the back button) or by calling finish() the view loads once again. It only closes if I tap the back button again. What may be the cause of this?

It's probably because you're using an OnTouchListener, which triggers on press, move and release. Try using an OnClickListener instead.

This should not give you problems:
newEntryButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent activityChangeIntent = new Intent(MainActivity.this,NewSpendingActivity.class);
MainActivity.this.startActivity(activityChangeIntent);
}
});

Related

Activity closes when double click a CardView to open

I have MainActivity which has a cardView inside a recyclerView and I want to double click on the card view to open the SecondActivity But when I touch the cardView twice very quickly I see that the app completely closes and I see android home screen and apps screen But when I touch the app launcher icon again I see the second activity.
(I have handled twice click with a boolean flag)
MyCode:
recyclerView onClick event:
#Override
public void onClick(final View view) {
if (AppActivity.isResponding()) {
Log.i("MainActivity", "onClick: applied");
Intent intent = new Intent(MainActivity.this, ProductGroupDetailActivity.class);
intent.putExtra(ProductGroupDetailActivity.EXTRA_PRODUCT_GROUP_NUMBER, (Integer) view.getTag(R.id.tag_productGroupNumber));
MainActivity.this.startActivity(intent);
AppActivity.setResponding(false);
}
}
SecondActivity:
#Override
protected void onResume() {
super.onResume();
responding = true;
}
what's the problem?
thank you

onCreate is called twice

I have 2 activities - MainActivity and page1. I have intent from the first like this:
button_forward.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Intent intent = new Intent(MainActivity.this, Page1.class);
startActivity(intent);
return true ;
}
However, my Page1 class's onCreate is called twice! i can detect it with toast message: it appears twice on the screen:
#Override
protected void onCreate(Bundle savedInstanceState) {
Toast.makeText(Page1.this, "onCreate 1", Toast.LENGTH_SHORT).show();
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
//some code here like findview by id...
}
});`
I do have some methods that use countdowntimer in my Page1 activity, timers that wait for 2 secs, i commented them out, but onCreate() still gets called twice.
I added code to prevent screen rotating, but the error persists
The problem is that your touch listener doesn't check which type of event we have, this means that your onTouch should be called like 100 times per second, but it actually is called only twice because the new Activity UI covers the button. Use this code to check which event we have:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN) return false;
Intent intent = new Intent(MainActivity.this, Page1.class);
startActivity(intent);
return true;
}
If the action is ACTION_DOWN it means that the user has just touched the button. If it is ACTION_UP it means that the user lifted his / her finger.

Android Back Button to specific activity

When I click the back button Android goes to the previous activity.
Is it possible to set for every activity a custom (back) activity
or to set the back button to the home menue of the app?
Help or hints would be great :)
You will have to override onBackPressed() from your activity:
#Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(ThisActivity.this, NextActivity.class));
finish();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent i = new Intent(this.class, yourcustomclass);
startActivity(i);
finish();
}
}
Yes it's possible, just add this method to your activity:
public void onBackPressed() {
//Do the stuff you want on backbutton pressed.
}
Yes you should #override the onBackPressed() function and create an Itent to go wherever you want.
You can override the
#Override
public void onBackPressed(){
}
If you need to go back what ever activity, when click on ActionBar back arrow(Home). overide onSupportNavigateUp()
#Override
public boolean onSupportNavigateUp() {
//onBackPressed(); //this will be go to parent activity
//******************//
// Your intent here //
//******************//
return true;
}

Long Touch handling in Android

I want to open dialog activity on long touch of current activity.
I have accomplished it on simple touch event but i want to perform the same on long touch so that if user is touched the screen by mistake it will not affect on app.
How to acieve it?
Any help is appriciated.
You don't touch an activity but a view that is shown by your activity. You can set a View.OnLongClickListener with the setOnLongClickListener method.
I want to open dialog activity on long touch of current activity.
You can not set the event on click of an Activity you can set the event on the Particular layout or a view(the root layout is better if you want to set it for whole activity).
you can set event on LongClick on view or layout by using following line.
yourViewOrLayout.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//The Action you want to perform
return false;
}
});
hope this helps.
Try this :
RelativeLayout rl=(RelativeLayout) findViewById(R.id.relative1);
rl.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return false; // do false here
}
});
EDIT :
Intent loginIntent = new Intent(ypurActivity.this, Login.class);

onTouch is triggered again on back button pressed

I have this code in the onCreate method:
ImageView iv01 = (ImageView)findViewById(R.id.hexagon01);
iv01.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View view, MotionEvent event) {
Intent intent = new Intent(view.getContext(), ChoiceActivity.class);
startActivity(intent);
return true;
}
});
When I touch the image the new activity loads correctly. I can go back pressing back button. But when I press the back button again, to close the app, it launches again the onTouch event, loading again the activity. How can I avoid it?
Thanks.
OnTouchListener gets triggered for several reasons (you need to check the MotionEvent parameter in order to find the exact reason). It seems you should just use an OnClickListener and achieve the same thing in an easier way.
When I touch the image the new activity loads correctly. I can go back pressing back button. But when I press the back button again, to close the app, it launches again the onTouch event, loading again the activity.
Honestly, the back button doesn't launch new copies of your Activity. Your OnTouchListener launches a new copy of the Activity for each ACTION_DOWN, ACTION_MOVE, and ACTION_UP MotionEvent. But you only notice the numerous Activities when you try to close the active Activity...
Simply ensure that you only launch the new Activity on one MotionEvent:
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
Intent intent = new Intent(view.getContext(), ChoiceActivity.class);
startActivity(intent);
return true;
}
return false;
}
Or you could use an OnClickListener here instead.

Categories

Resources