Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot pop() more than once from the static stack
    text
    copied!<p>I need to use a static stack in one of my android application. Here's how i used it:</p> <p>In the Constants File: </p> <pre><code>public static Stack&lt;String&gt; st = new Stack&lt;String&gt;(); </code></pre> <p>And in the Activity File, inside the onBackPressed method following logic is used:</p> <pre><code>if (!Constants.st.isEmpty()) { Log.i("CHECK", "Stack is not empty."); Log.v("CHECK", "PEEK: " + Constants.st.peek()); Constants.st.pop(); if(Constants.st.isEmpty()){ Log.i("CHECK", "Stack is empty."); }else{ Log.v("CHECK", "PEEK after POP: " + Constants.st.peek()); } }else{ Log.i("CHECK", "Stack is empty."); } </code></pre> <p>Now when i press back button once it works (i mean pop() function works), then second time it( pop() function ) doesn't. Here is the Log of the above logic:</p> <pre><code>: Stack is not empty. : PEEK: UI_FirstActivity : Stack is empty. : Stack is not empty. : PEEK: UI_SecondActivity : PEEK after POP: UI_FirstActivity : Stack is not empty. : PEEK: UI_FirstActivity : PEEK after POP: UI_FirstActivity </code></pre> <p>PS: Another thing is that whenever all these UI.. functions contains different dynamically created forms shown on the same activity. Now when we enter/show any method/form its name i.e., UI.. is been pushed in the stack.</p> <p>PPS: I am fine with downvote, but a simple explanation will be very helpful. </p> <p>PPPS: It seems that <strong>EJB</strong> is assuming that i am assuming :-). I will again try to explain what exactly is happening: I have an <strong>activity</strong> in which i am choosing to show various forms but one at a time. each method UI_First... represents one form. now what happens is that sometimes one of the form is skipped from showing because it contains no views for example UI_Second... So after UI_First... UI_Third form is shown. Now when the user presses the back button ideal situation is that program will skip the UI_Second.. (because it contains no views) and open UI_First... To implement this functionality i used a stack where whenever a form is shown i.e., when UI_First... method is called its name is pushed in the stack. and when back button is pressed it is popped. I apologize if i was not clear enough. </p> <p>PPPPS: i got the problem 10 mins ago. it was exactly was <strong>pratik</strong> is saying in the answer so i am going to accept his answer.</p>
 

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