Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:52:01+00:00 2026-06-11T18:52:01+00:00

I have four activities in my application A–>B–>C–>D. I write a My own class

  • 0

I have four activities in my application A–>B–>C–>D.

I write a My own class MyActivity which extends Activity class. I write this class to handle activity stack.This class has two methods addActivitiyTostack() and getActivityFromStack()
I used a stack for storing activities.

All other activities are extends this class.
When I moved from one activity to other using intent it added to stack.

And when I moved backword activity gets popped up.
I can correctly add activities to stack, But I have problem in popping the activities.

also I have Logout Button on all activities, OnClick of this button I want to close the application how to implement it? Anybody know how to handle activity stack in Android.

This is my code.

package com.example.iprotect;

import java.util.Enumeration;
import java.util.Stack;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MyActivity extends Activity {

    private static Stack<Activity> stack = new Stack<Activity>();
    static int top=0;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addActivityToStack(this);
    }


    public static void addActivityToStack(MyActivity myActivity) {
        // TODO Auto-generated method stub
        stack.push(myActivity);
        for (int i =0; i< stack.size() ; i++) {
            Activity act=stack.get(i);
            Log.i("Element in stack", ""+act);
        }
    }


    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        //getActivityFromStack();
        //logoutFromApplication();

    }


    public static void logoutFromApplication() {
        // TODO Auto-generated method stub
        Enumeration<Activity> enm=stack.elements();

        while(enm.hasMoreElements())
        {
            Activity act=enm.nextElement();
            stack.pop();
        }

    }


    public static Activity getActivityFromStack() {
        return stack.pop();
    }

}

A–>

public class WebServiceActivity extends MyActivity{

    EditText editText1, editText2;
    Button button;
    String response = "";
    String email, password;

    public final Pattern EMAIL_ADDRESS_PATTERN = Pattern
            .compile(".+@.+\\.[a-z]+");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText editText1 = (EditText) findViewById(R.id.etEmail);
        final EditText editText2 = (EditText) findViewById(R.id.etPassword);

        button = (Button) findViewById(R.id.loginButton);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(editText1.getText().toString())) {
                    Toast.makeText(getApplicationContext(),
                            "please enter email id", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(editText2.getText().toString())) {

                    Toast.makeText(getApplicationContext(),
                            "please enter passwod", Toast.LENGTH_SHORT).show();
                } else {
                    Boolean bool = EMAIL_ADDRESS_PATTERN.matcher(
                            editText1.getText().toString()).matches();
                    if (bool == true) {

                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Invalid email id", Toast.LENGTH_SHORT).show();
                    }

                    email = editText1.getText().toString();
                    password = editText2.getText().toString();
                    // final ProgressDialog pd = ProgressDialog.show(
                    // WebServiceActivity.this, "Calling webservice...",

                    // "Please wait...", true, false);

                    final ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar2);
                    bar.setVisibility(View.VISIBLE);

                    new AsyncTask<Void, Void, Void>() {
                        String r;

                        protected void onPreExecute() {
                        };

                        @Override
                        protected Void doInBackground(Void... params) {
                            r = invokeWebService();

                            return null;
                        };

                        protected void onPostExecute(Void result) {
                            bar.setVisibility(View.VISIBLE);

                        };

                    }.execute();

                }
            }

            private String invokeWebService() {
                String response = "";
                try {
                    WebService webService = new WebService(
                            "http://sphinx-solution.com/iProtect/api.php?");
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("action", "auth");
                    params.put("email", email);
                    params.put("password", password);
                    response = webService.WebGet("auth", params);

                    JSONObject jsonObject = new JSONObject(response);
                    String rr = jsonObject.optString("status");

                    if (TextUtils.equals(rr, "success")) {
                        Log.e("MSG", "status==success");
                        Intent intent = new Intent(WebServiceActivity.this,
                                SecondActivity.class);
                        //MyActivity.addActivityToStack(WebServiceActivity.this);
                        intent.putExtra("email", email);
                        intent.putExtra("password", password);
                        WebServiceActivity.this.startActivity(intent);
                        finish();

                    } else {
                        Log.e("MSG", "status ==failed");

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return response;
            }
        });
    }
}

B–>

public class SecondActivity extends MyActivity {

    ListView listView;
    String email1, password1;
    ArrayList<JSONStructure> arrayList = new ArrayList<JSONStructure>();
    String r;
    String r1;
    String tablename;
    String rows;
    JSONObject jsonObject;
    String tablename2;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);

        Intent intent = getIntent();
        email1 = intent.getExtras().getString("email");
        password1 = intent.getExtras().getString("password");

        Button btn1 = (Button) findViewById(R.id.refreshbutton);
        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final ProgressDialog pd = ProgressDialog.show(
                        SecondActivity.this, "Refresh List...",
                        "Please wait...", true, false);

                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {
                        pd.dismiss();
                        r = invokeWebService();
                        return null;
                    }

                    protected void onPostExecute(Void result) {
                    };

                }.execute();

            }
        });

        Button btn = (Button) findViewById(R.id.logoutbutton);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final ProgressDialog pd = ProgressDialog.show(
                        SecondActivity.this, "Calling webservice...",
                        "Please wait...", true, false);

                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {
                        pd.dismiss();
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        Intent intent = new Intent(SecondActivity.this,
                                WebServiceActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        SecondActivity.this.startActivity(intent);

                    }

                }.execute();

            }

        });

        listView = (ListView) findViewById(R.id.listview1);

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                    int position, long id) {

                JSONStructure jsonstructure = (JSONStructure) listView
                        .getAdapter().getItem(position);

                final String tablename1 = jsonstructure.getTableName()
                        .toString();

                Intent intent = new Intent(SecondActivity.this,
                        ProgressBarActivity.class);
                //MyActivity.addActivityToStack(SecondActivity.this);

                intent.putExtra("tablename", tablename1);
                intent.putExtra("Rows", rows);
                intent.putExtra("email", email1);
                intent.putExtra("password", password1);

                SecondActivity.this.startActivity(intent);

            }

        });

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {

                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
                r = invokeWebService();

                try {
                    JSONArray jsonArray = new JSONArray();
                    JSONObject jsonObject = new JSONObject(r);
                    jsonArray = jsonObject.getJSONArray("Records");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject c = jsonArray.getJSONObject(i);
                        tablename = c.optString("TABLE NAME");
                        rows = c.optString("Rows");
                        JSONStructure jsonStructure = new JSONStructure();
                        jsonStructure.setTableName(tablename);
                        jsonStructure.setRows(rows);
                        arrayList.add(jsonStructure);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

                return null;

            }

            @Override
            protected void onPostExecute(Void result) {
                if (arrayList != null && arrayList.size() > 0) {

                    MyAdapter adapter = new MyAdapter(SecondActivity.this,
                            arrayList);
                    listView.setAdapter(adapter);
                }
            }

        }.execute();

    }

    private String invokeWebService() {
        String response = "";
        try {
            WebService webService = new WebService(
                    "http://sphinx-solution.com/iProtect/api.php?");
            Map<String, String> params = new HashMap<String, String>();
            params.put("action", "getTables");
            params.put("email", email1);
            params.put("password", password1);
            response = webService.WebGet("getTables", params);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

}

C–>

public class ThirdActivity extends MyActivity {
    String tablename1, row1, json1;
    ArrayList<JSONStructure> arrayList = new ArrayList<JSONStructure>();

    JSONArray jsonArray, jsonArray2;
    JSONObject jsonObject;
    String row;
    String email1, password1;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.third_layout);

        ListView listView = (ListView) findViewById(R.id.listview2);

        TextView textView = (TextView) findViewById(R.id.title_textview);

        Intent intent = getIntent();
        tablename1 = intent.getExtras().getString("tablename");
        row = intent.getExtras().getString("Rows");
        textView.setText(tablename1);
        json1 = intent.getExtras().getString("Json");
        email1 = intent.getExtras().getString("email");
        password1 = intent.getExtras().getString("password");

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {

                Button button = (Button) findViewById(R.id.goback);
                button.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(ThirdActivity.this,
                                SecondActivity.class);
                        MyActivity.getActivityFromStack();
                        intent.putExtra("email", email1);
                        intent.putExtra("password", password1);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        ThirdActivity.this.startActivity(intent);

                    }
                });

            }

        }.execute();

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                    final int position, long id) {
                final ProgressDialog pd = ProgressDialog.show(
                        ThirdActivity.this, "Calling webservice...",
                        "Please wait...", true, false);

                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... paramArrayOfParams) {

                        pd.dismiss();

                        try {

                            jsonObject = new JSONObject(json1);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        Intent intent = new Intent(ThirdActivity.this,
                                FinalActivity.class);
                        //MyActivity.addActivityToStack(ThirdActivity.this);
                        intent.putExtra("tablename", tablename1);
                        intent.putExtra("Json", jsonObject.toString());
                        intent.putExtra("Row", position);
                        intent.putExtra("email", email1);
                        intent.putExtra("password", password1);
                        ThirdActivity.this.startActivity(intent);

                    }

                }.execute();

            }

        });

        try {

            JSONObject jsonObject = new JSONObject(json1);
            JSONArray jsonArray = new JSONArray();
            jsonArray = jsonObject.getJSONArray("FirstThree");
            jsonArray2 = jsonObject.getJSONArray("Color");

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject c = jsonArray.getJSONObject(i);
                String one = c.optString("One");
                String two = c.optString("Two");
                String three = c.optString("Three");

                JSONObject c1 = jsonArray2.getJSONObject(i);

                String color = c1.optString("color");

                JSONStructure jsonStructure = new JSONStructure();
                jsonStructure.column1 = one;
                jsonStructure.column2 = two;
                jsonStructure.column3 = three;
                jsonStructure.setColumn1(one);
                jsonStructure.setColumn2(two);
                jsonStructure.setColumn3(three);
                jsonStructure.setColor(color);

                arrayList.add(jsonStructure);
                Log.e("one", c.optString("One"));
                Log.e("two", c.optString("Two"));
                Log.e("three", c.optString("Three"));
                Log.e("color", c1.optString("color"));

            }

        } catch (Exception e) {
            e.printStackTrace();

        }

        if (arrayList != null && arrayList.size() > 0) {

            MyAdapter1 adapter1 = new MyAdapter1(ThirdActivity.this, arrayList);
            listView.setAdapter(adapter1);

        }

        Button btn = (Button) findViewById(R.id.logoutbutton);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final ProgressDialog pd = ProgressDialog.show(
                        ThirdActivity.this, "Calling webservice...",
                        "Please wait...", true, false);

                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {
                        pd.dismiss();
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        Intent intent = new Intent(ThirdActivity.this,
                                WebServiceActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        ThirdActivity.this.startActivity(intent);
                        super.onPostExecute(result);
                    }

                }.execute();

            }

        });

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:52:02+00:00Added an answer on June 11, 2026 at 6:52 pm

    Assuming you need this stack solely for the logout function, there are better ways. Use a broadcast instead. Register a BroadcastReceiver in MyActivity.onCreate. The receiver should just call the activity’s finish(). Send the broadcast from the button’s click listener (btn1? What does that button do? Couldn’t guess from the name; better names required 😉 ). That’s it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four activities namely, Demo_tabActivity.java [main activity] Tabhost.java The below two activities are
I have four activities namely, Demo_tabActivity.java [main activity] Tabhost.java The below two activities are
I am a beginner in android. I have four stages (four activities). In activity
I have four activities, A, B, C and D. app starts with activity A,
I need some help :) I have a tab activity with four sub activities.
I have four activity in my tasks A,B,C,D. Activities are launched in order A->B->C->D.
I have a Weather app with four Activities. The main/launcher activity is 'invisible' using...
I have four activities A,B,C,D. I am passing an text from activity A to
In my application,I have four tabs at the bottom.Each tab has multiple activities.In order
I have this scenario I have four classes User profiles Activity Workbook user can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.