-2

I want to start new Intent only if the previous one is finished, how can I do it..

here I want to goto purchase window if i am already registered, if I am not, then i should be there on Registration page. after finishing registration by clicking register button the purchase window should come.

now both activities starting!!!

if (!registered) {
//goto registration window
new MainActivity().registerUser(getContext());

    }

    //goto book purchase window
    new MainActivity().showPurchaseWinsow(getContext());

}

the registeruser() method is:

public void registerUser(Context context) {
    Intent registerUser = new Intent(context, RegisterUser.class);
    context.startActivity(registerUser);
}

the showPurchaseWindow is

public void showPurchaseWinsow(Context context) {
    Intent purchaseBook = new Intent(context, BookPurchaseMain.class);
    context.startActivity(purchaseBook);
}

whats wrong with it....any help please.. How can I start the purchaseWindow only after finishing the registration window.

laminatefish
  • 5,197
  • 5
  • 38
  • 70
Blue_Alien
  • 2,148
  • 2
  • 25
  • 29

4 Answers4

1
public boolean isRunning(Context ctx) {
    ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (RunningTaskInfo task : tasks) {
        if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName())) 
            return true;                                  
    }

    return false;
}

source : How to know activity has been finished?

you can check if the register activity is running or not from the above code. If the activity is not running directly call purchaseActivity else call purchaseActivity before finishing registerUserActivity i.e in your registerUserActivity

Community
  • 1
  • 1
sanedroid
  • 1,036
  • 4
  • 16
  • 27
0

Go to your registerUser Activity and at the end of your code add the intent to start the purchaseBook Activity

so you add this in your registerUser Activity

EDIT

Intent purchaseBook = new Intent(this, BookPurchaseMain.class);
    this.startActivity(purchaseBook);
Nadir Belhaj
  • 11,953
  • 2
  • 22
  • 21
  • I tried it, but it gives me some error.: "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?" – Blue_Alien Sep 16 '14 at 13:57
  • Edited change context with this – Nadir Belhaj Sep 16 '14 at 13:58
  • it was giving the above error while i am trying these code directly into the end, because i was trying it inside an onClickListener, i made slight change, instead of calling intent directly i made it a method inside registerActivity and called it, working fine . thanks – Blue_Alien Sep 18 '14 at 04:55
0

You shouldn't create an instance of your MainActivity. I don't know what the layout of your project looks like, but you need to write it so that you can call startActivity from an activity, or have a context like you do.

if (!registered) 
{
    //goto registration window
    Intent registerUser = new Intent(context, RegisterUser.class);
    context.startActivity(registerUser);

    }

     //goto book purchase window
    Intent purchaseBook = new Intent(context, BookPurchaseMain.class);
    context.startActivity(purchaseBook);

}
RyPope
  • 2,645
  • 27
  • 51
  • thanks, @RyPope bt,I am using these statements from a class which extends an abstract acitvity class.. can i do i from there? – Blue_Alien Sep 16 '14 at 13:54
0

If the user has not registered, he will be directed to RegisterUSer right! so call the purchaseBook class from registerUSer class and make the existing the call of Purchasebook in else loop in the current code.

So if the user registered the PurchaseBook class will be called. If not he will directed to RegisterUSer class and from there once user is done he/she will be navigated to PurchaseBook class whose call u made in RegisterUSer class.

Sid
  • 691
  • 6
  • 9
  • I tried it, but it gives me some error.: "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?" – Blue_Alien Sep 16 '14 at 13:59
  • is your exsiting RegisterUSer class is not an activity? – Sid Sep 16 '14 at 15:27
  • if your registerUser is not an activity you may get that problem, then use a constructor in your RegisterUSer class which have context parameter, pass the context parameter from your mainActivity to registerUSer and call myContext.startActivity in your RegisterUser class to invoke the purChaseBook class – Sid Sep 16 '14 at 15:32
  • it is already extending Activity class, but i tried it within a onClickListener of a button, is there any problem? – Blue_Alien Sep 17 '14 at 00:45