How to start a new activity (with intent) if i hold a button and get back to main activity if i release my finger?

428 views Asked by At

I got an imageview which starts a new activity.

//Mainactivity
imageView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            // start your activity...
            Intent i = new Intent( MainActivity.this, FullActivity.class);
            startActivity(i);
            return false;
        }
    });

I prefer to still hold my finger on the screen and if i realease my finger i get back to my MainActivity. I tried it with an ontouchlistener but it didnt recognize that i´m still holding the screen after the new activity start.

//Fullactivity
imageViewFull.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()== MotionEvent.ACTION_UP){

                finish();
            }
            return true;
        }
    });

How to integrate that function?

1

There are 1 answers

0
Mircea Mărilă On

I encountered the same problem and the only solution I found was to hold the button and peek at the previous activity for 3 seconds. Here's how i did it: Let's say that button1 from MainActivity sends you to the SecondActivity and by holding button2 in SecondActivity you can peek some seconds at MainActivity. So, in MainActivity, we assign to button1 a setOnClickListener:

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // start SecondActivity for result
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("string", string); // variables you want to pass
        startActivityForResult(intent, REQUEST_CODE); // REQUEST_CODE = 1 (declared above onCreate)
    }
});

In SecondActivity we assign setOnClickListener and setOnLongClickListener to button2:

button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent resultIntent = getIntent();
        resultIntent.putExtra("peek", "false");
        setResult(Activity.RESULT_OK, resultIntent);
        finish();
    }
});

button2.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        Intent resultIntent = getIntent();
        resultIntent.putExtra("peek", "true");
        setResult(Activity.RESULT_OK, resultIntent);
        finish();
        return true;
    }
});

We pass the string "peek" for knowing in MainActivity if we want to peek or to fully return. Finally, we must implement onActivityResult in MainActivity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE  && resultCode  == RESULT_OK)
        {
            String peek = data.getStringExtra("peek");
            if(peek.equals("false")) //we dont return to SecondActivity
            {
                //your code
            }
            else { 
                //we stay on MainActivity for 3 seconds and return to SecondActivity
                //your code

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                        intent.putExtra("string", string); // variables you want to pass
                        startActivityForResult(intent, REQUEST_CODE);
                    }
                }, 3000); //time in milliseconds 
            }
        }
    } catch (Exception ex) {
        //nothing
    }

}

Well, that's it! I hope this is usefull :D