Same activity getting stacked

133 views Asked by At

I have an activity that is suppose to finish itself and close the application. Now, in certain cases, which are varying on how the user is navigating to the activity, the activity is getting stacked. When the activity is stacking up, then calling finish() or android.os.Process.killProcess(android.os.Process.myPid()); or both together is only showing up the same activity again.

The Manifest:

<activity
            android:name="newActivities.HomeActivity"
            android:label="@string/title_activity_home"
            android:screenOrientation="portrait">
        </activity>

The activity:

public class HomeActivity extends Activity {

    private EditText studentNameEdittext;
    private Button startYourStoryButton, loginButton, navCollegesButton, settingsButton, search_friends_button, browseStoriesButton;
    private TextView textView1;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
//      if (getFromPreference("loginStatus").equalsIgnoreCase("true")) {
//          finish();
//          saveInPreference("loginStatus", "");
//      }
        // Set up the action bar
        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#009945")));
        bar.setTitle("Study Story");
        bar.setIcon(R.drawable.statusbar_icon);
        int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
        TextView yourTextView = (TextView) findViewById(titleId);
        yourTextView.setTextColor(getResources().getColor(R.color.white_colour));
        yourTextView.setTypeface(TypeFaceController.titleFace(HomeActivity.this));



        // studentNameEdittext = (EditText)
        // findViewById(R.id.studentNameEdittext);
        startYourStoryButton = (Button) findViewById(R.id.startYourStoryButton);
        // loginButton = (Button) findViewById(R.id.loginButton);
        navCollegesButton = (Button) findViewById(R.id.navCollegesButton);
        // settingsButton = (Button) findViewById(R.id.settingsButton);
        // search_friends_button = (Button)
        // findViewById(R.id.search_friends_button);
        browseStoriesButton = (Button) findViewById(R.id.browseStoriesButton);
        // textView1 = (TextView) findViewById(R.id.textView1);

        // Set up font type
        // studentNameEdittext.setTypeface(TypeFaceController.generalTextFace(HomeActivity.this));
        startYourStoryButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
        // loginButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
        navCollegesButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
        // settingsButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
        // search_friends_button.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
        browseStoriesButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
        // textView1.setTypeface(TypeFaceController.titleFace(HomeActivity.this));

        startYourStoryButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0){
                Intent i = new Intent(HomeActivity.this, SignUp.class);
                i.putExtra("signUpCaller", "Home");
                startActivity(i);
            }
        });

        // loginButton.setOnClickListener(new View.OnClickListener() {
        //
        // @Override
        // public void onClick(View arg0){
        // Intent i = new Intent(HomeActivity.this, Login.class);
        // startActivity(i);
        // }
        // });

        // search_friends_button.setOnClickListener(new View.OnClickListener() {
        //
        // @Override
        // public void onClick(View v){
        // Intent i = new Intent(HomeActivity.this,
        // FindStudentBrowseStoryActivity.class);
        // i.putExtra("Button", "search_friends_button");
        // i.putExtra("searchString", studentNameEdittext.getText().toString());
        // startActivity(i);
        //
        // }
        // });

        browseStoriesButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v){
                Intent i = new Intent(HomeActivity.this, FindStudentBrowseStoryActivity.class);
                i.putExtra("Button", "browseStoriesButton");
                startActivity(i);

            }
        });

        navCollegesButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v){
                Intent i = new Intent(HomeActivity.this, CollegeListActivity.class);
                startActivity(i);

            }
        });

        // settingsButton.setOnClickListener(new View.OnClickListener() {
        //
        // @Override
        // public void onClick(View v){
        // Intent i = new Intent(HomeActivity.this, HomeSettingsActivity.class);
        // i.putExtra("FromActivity", "HomeSettingsActivity");
        // startActivity(i);
        // finish();
        //
        // }
        // });

    }

//  @Override
//  protected void onStart(){
//      if (getFromPreference("loginStatus").equalsIgnoreCase("true")) {
//          finish();
//      }
//      super.onStart();
//  }
//
//  @Override
//  protected void onResume(){
//      if (getFromPreference("loginStatus").equalsIgnoreCase("true")) {
//          finish();
//      }
//      super.onResume();
//  }

    // =========Login button action bar
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_menu, menu);
        return true;
    }

    // =========Login button action bar
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        // handle item selection
        switch (item.getItemId()) {
        case R.id.home_login_string:
            Intent i = new Intent(HomeActivity.this, Login.class);
            // finish();
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    // logic to fix logout

    @Override
    public void onBackPressed(){
//      Intent startMain = new Intent(Intent.ACTION_MAIN);
//      startMain.addCategory(Intent.CATEGORY_HOME);
//      startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);;
//      startActivity(startMain);
//
//       int pid = android.os.Process.myPid(); //
//       android.os.Process.killProcess(pid); // return; }
        android.os.Process.killProcess(android.os.Process.myPid());
        finish();
    }

    // method to save variable in preference
    public void saveInPreference(String name, String content){
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(name, content);
        editor.commit();
    }

    // getting content from preferences
    public String getFromPreference(String variable_name){
        String preference_return;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        preference_return = preferences.getString(variable_name, "");

        return preference_return;
    }
}

Please tell me where am I going wrong? Why is the activity stacking?

P.S: We cannot use single top etc as it causes some transition issues with the existing custom theme!

4

There are 4 answers

0
kittu88 On BEST ANSWER

At last, the problem is solved. I has to do a little trick:

ParseUser.getCurrentUser();
        ParseUser.logOut();
        Intent i = new Intent(getActivity(), NewHomeActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        getActivity().startActivity(i);
        getActivity().finish();

This did not cause any transition issues as well.

3
BandaruNaresh On

Take one application class which extends Application and take one arrayList and maintain the references of the activities in the arraylist.

When you click on back button in desired activity then finish the all the activities using arraylist.

Take one base activity. Which is super class of all the activities

public class BaseActviity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    App application = (App) getApplication();
    application.addActivity(this);
  }
}

Take one application class 

public class App extends Application {
public App() {
    if (listActivty == null) {
        listActivty = new ArrayList<BaseActviity>();
  }
}

public ArrayList<BaseActviity> listActivty;

public void addActivity(BaseActviity actviity) {
    if (!listActivty.contains(actviity)) {
        listActivty.add(actviity);
    }
  }
 } 

take 4 samples activity classes like ......

1) public class FirstActivity extends BaseActviity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first);
}

public void send(View view) {
    Intent intent = new Intent(this, SecondActivity.class);
    startActivity(intent);
 }
}
2) public class SecondActivity extends BaseActviity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first);
 }

public void send(View view) {
    Intent intent = new Intent(this, ThirdActivity.class);
    startActivity(intent);
 }
}
 3).........................
 4) ............................
 in 4 th activity class placed the following code

In desire activity that means final activity,override the onBackPressed

 public void onBackPressed() {

    super.onBackPressed();
    App application = (App) getApplication();
    ArrayList<BaseActviity> listActivty = application.listActivty;
    for (BaseActviity actviity : listActivty) {
        actviity.finish();

     }
}
0
Anuj On

Check if the Activity, that would be last activity when user clicks on the back button,is visible or not and if visible use

System.exit(0);
2
Sarthak Mittal On

Wherever you are opening the activity which is getting stacked up, use this:

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);