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.
Related
I have a RecyclerViewClickListener.RecyclerTouchListener and on that #onClick override method, I sat Intent to go another activity.
But the problem is on that RecyclerViewClickListener.RecyclerTouchListener there is one image view also and I implemented onClickListener of that InmageView so that it will open one dialog box.
Now, when I click on that ImageView, 2 things happened at same time.
1.Opened Dialog box
2.It is going to another activity also, because of Intent.
How can I fix?
Try this
holder.hamburgerMenu.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if (menuInterface != null) {
menuInterface.onClickHamburger(position);
}
return true;
}
return false;
}
});
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);
}
});
I have an ImageView inside a ScrollView. The ImageView is clickable and opens up a new Activity.
Whenever I scroll through my ScrollView first touching the image (But not releasing my finger yet) and try to scroll it fires my onTouch() or onClick() method (I have tried with both...)
Here is my code:
btnAdd.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Intent intent = new Intent(Intent.ACTION_EDIT);
startActivity(intent);
return true;
}
});
As I said I also tried with onClick()...
What am I doing wrong?
When the OnTouchListener is triggered it listen for multiple actions (Action Down, Action Up, etc) and executes them all so you should specify exactly when you want to start the new activity (in your case on Action Up). So try to use this:
btnAdd.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP){
Intent intent = new Intent(Intent.ACTION_EDIT);
startActivity(intent);
}
return true;
}
});
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.
I want to know that if I have two activities in a program then how can I switch between two activities using ontouchlistener, just only touching anywhere on the screen?
public class V19 extends Activity implements OnTouchListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay19);
}
#Override
public boolean onTouch(View to_main, MotionEvent event) {
return false;
}
}
You can use layout inflater and method setContentView(View v)
public class V19 extends Activity implements OnTouchListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = getLayoutInflater().inflate(R.layout.lay19, null);
setContentView(view);
view.setOnTouchListener(this);
}
#Override
public boolean onTouch(View to_main, MotionEvent event) {
Intent i = new Intent(this, Activity2.class);
startActivity(i);
return false;
}
}
And than you can catch all touch events.
Other way is to override public boolean dispatchTouchEvent (MotionEvent ev) method. Reference says:
public boolean dispatchTouchEvent (MotionEvent ev)
Since: API Level 1
Called to process touch screen events. You can override this to intercept all touch screen events before they are dispatched to the window. Be sure to call this implementation for touch screen events that should be handled normally.
Parameters
ev The touch screen event.
Return true if this event was consumed.
You will need to pin a listener to different aspects in your layout (buttons, etc.)
To have the device pick up touch events anywhere, I've used public boolean onTouchEvent(final MotionEvent event), which will hold the entire event performed (how many fingers, position, etc.)
by following this tutorial you should be able to move from one to the other.
The tutorials uses a button click but it should be possible to replace that with what you want.
Basically you need to have your activity made(which I assume you do)
Call your intent and activity onTouch instead of on a button click.
And don't forget to declare your new activity in the manifest.
<activity android:name=".YourActivityName"></activity>