Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid + how to interrupt the loop of handler(run())
    primarykey
    data
    text
    <p>I have 4 Classes,</p> <ol> <li>FirstActivity</li> <li>Play_1</li> <li>Play_2</li> <li>Play_3</li> </ol> <p>onClicking a button in <code>FirstActivty</code>, I'll go randomly to the remaining three activities. For example, in the following code I came to <code>Play3.class</code>... I have set then handler to run for 3 seconds. My problem is during the <code>run()</code> time of 3 seconds, if I click the <code>backButton</code>, it will go to <code>FirstActivity.class</code> and after 3 seconds of <code>run()</code> time , it is going to the random loop again (<code>Play_1</code>, <code>Play_2</code>, <code>Play_3</code>). Now, I want to go permanently to the <code>FirstActivity.class</code> only when I click <code>backButton</code>. What Should I do?</p> <p>(NOTE: After completion of <code>run()</code> time of three seconds, I am happily going to home screen. But, During that <code>run()</code> Time only I want to Interrupt the loop and go back to the HOME screen.)</p> <pre><code>class listener implements OnClickListener { @Override public void onClick(final View v) { switch (v.getId()) { case R.id.play3_image1: bt1.setVisibility(View.GONE); bt2.setVisibility(View.GONE); bt3.setVisibility(View.GONE); bt4.setVisibility(View.GONE); bt5.setVisibility(View.GONE); bt6.setVisibility(View.VISIBLE); try { new Handler().postDelayed(new Runnable() { @Override public void run() { Random rand = new Random(); int x = rand.nextInt(3) + 1; Intent intent = new Intent(); switch (x) { case 1: intent.setClass(v.getContext(), Play_1.class); break; case 2: intent.setClass(v.getContext(), Play_2.class); break; case 3: intent.setClass(v.getContext(), Play_3.class); break; default: break; } startActivity(intent); } }, 3000); } catch (Exception e) { e.printStackTrace(); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; event.getRepeatCount() == 0 ){ Intent i = new Intent(Play_3.this,Example.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); } return super.onKeyDown(keyCode, event); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload