1

I have created an android application which has two activities, one of them is MainActivity and the other one is Article activity

In this application you can open unlimited number of activities. So it's like an article app, inside an article there are also other articles so you can follow unlimited number of articles. Every article opens a new Article activity.

Now what I want to do is this: When the 10th Article is opened I want to close the first Article activity (not MainActivity).

I have also read this question, but it works only if you have different activities (i.e. ActivityA, ActivityB, ActivityC...).

Is there any way I can do this?

Community
  • 1
  • 1
Idrizi.A
  • 9,819
  • 11
  • 47
  • 88
  • 2
    If you build your app on fragments instead you could simply use a counter when adding the fragments to the back stack and remove old tags. Consider: http://stackoverflow.com/questions/22474584/remove-old-fragment-from-fragment-manager – JohanShogun Aug 19 '15 at 20:39
  • I see that I can do it in `fragments`, but actually I am not using them, I am currently using `Activity`. – Idrizi.A Aug 19 '15 at 20:47
  • I'm aware of that, I'm suggesting you use fragments instead since it's a better fit for what you are trying to do. Won't be a difference to the end user, only programmatically. – JohanShogun Aug 19 '15 at 20:48
  • 1
    Kind of like this scenario: "I eat soup with my fork and it keeps spilling out, how can I remove the holes?" -- "use a spoon" -- "I'm not using a spoon, I'm using a fork". ;) sometimes there are other tools better situated for the task at hand. :) – JohanShogun Aug 19 '15 at 20:55
  • Yes but in the `Activity` it's actually `FragmentActivity`, there are three fragments in it. So if I want to make it like that I must use nested fragments which is going to take me too much time, and that is something I don't have. And I'm not sure if I can do this in API 9, which is my applications `minSdkVersion`. – Idrizi.A Aug 19 '15 at 21:11
  • 1
    Nested fragments should be fine, just use he latest compatibility library. Also, it's going to take much less time to fix your code than find and write an appropriate hack (that is reliable and future proof), the activity flag in the answer below won't help you since it would clear all instances of your activity (won't have a stack of 10) and calling finish on a non top activity is bad practice. So, good luck. – JohanShogun Aug 20 '15 at 05:41

2 Answers2

1

When opening 10th article, do this:

Intent intent = new Intent(this,Your_Article.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

or if you only want to close a particular activity, get the instance of that activity and close it using finish().

Aakash
  • 5,181
  • 5
  • 20
  • 37
0

this is a hack only to get it working but you may want to try fragment from your description;

  1. Create a new class called ActivityHandler.java

    public class ActivityHandler { 
    private static ActivityHandler uniqueInstance;
    private static List<Activity> mListActivity;
    private static int SIZE_LIMIT = 10;
    
    public static ActivityHandler getInstance() {
        if (uniqueInstance == null) {
            synchronized (ActivityHandler.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new ActivityHandler();
                }
            }
        }
        return uniqueInstance;
    }
    
    
    
    private Activityhandler() {
        if (mListActivity == null ) {
            mListActivity = new ArrayList();
        }
    }
    
    
    public static void add (Activity activity){
        mListActivity.add(activity);        
        if (mListActivity.size() > 10){
            Activity firstActivity = mListActivity.remove(0);
            firstActivity.finish();         
        }
    }   
    }
    

In your ArticleActivity's OnCreateMethod:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.article_activity);
    ActivityHandler.getInstance().add(this);


}