1

Can we start 2 activities using Intent from the same class in Android??

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Sai Krishna
  • 39
  • 1
  • 7
  • Yes, we can start as much Activities as we want. Just use the straightforward way. – Egor Sep 18 '12 at 14:32
  • 1
    What do you want to do? I think you want to accomplish two tasks and both of them might not be needing activities. Use threads to do parallel tasks – Mukesh Soni Sep 18 '12 at 14:32
  • AFAK, the screen would only show one activity at a time, so what do you mean by "2 activities"? – Huang Sep 18 '12 at 14:32
  • I would start one Activity and wait for the occurence of some event and then start second activity – Sai Krishna Sep 18 '12 at 14:36

2 Answers2

0

Yes, you can launch two different Activities from the same class:

Intent one = new Intent(getBaseContext(), FirstOther.class);
Intent two = new Intent(getBaseContext(), SecondOther.class);

if(startFirst) {
    startActivity(one);
} else {
    startActivity(two);
}

However, keep in mind that it can only be one at a time. You cannot have both of them visible at the same time.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • That isn't possible. Why do you need to do this? There is probably a better, and possible way to achieve what you want. – Raghav Sood Sep 18 '12 at 14:39
  • I would wait in the second Activity for displaying any layout.. But I want to run it in Background – Sai Krishna Sep 18 '12 at 14:43
  • Why? What are you trying to do that needs two activities to run simultaneously. – Raghav Sood Sep 18 '12 at 14:45
  • I am developing an android app for file transfer for devices under same network.. I want to display the devices and if any user is trying to send a file I should ask him for permission...For that I need to run another activity in the background – Sai Krishna Sep 18 '12 at 14:48
  • 1
    Why not use a dialog to ask for permission? – Raghav Sood Sep 18 '12 at 14:51
0

You can start as many activities as you want from a class. But once an activity is started it will take over. In short you can start one activity at a time from a class but that one could be any activity.

So depending upon some condition you can say

    if(x==1)
       Intent intent = new Intent(getApplicationContext(), Activity1.class);
    else
       Intent intent = new Intent(getApplicationContext(), Activity2.class);
    startActivity(intent);
xaragen
  • 153
  • 3
  • 10
  • I want to start both activities at the same time. – Sai Krishna Sep 18 '12 at 14:45
  • Ok see the problem is that any activity is something that is visible on your screen (in layman's terms). So since you have one screen you can not show both of them together. However suppose you want to do something like for example: `if(x==1)` `dosomething + dosomething_more` To achieve that you can start a thread with an activity that will do the background work while you display something on screen. It would be best if you explain your problem in more detail. As to what you are trying to accomplish. – xaragen Sep 18 '12 at 14:54
  • I am developing an android app for file transfer for devices under same network.. I want to display the devices and if any user is trying to send a file I should ask him for permission...For that I need to run another activity in the background – Sai Krishna Sep 18 '12 at 15:12
  • So what you need is a dialog box and a thread if permission is granted. I assume that if somebody initiates the transfer you want the person on the receiving end to allow the download? If yes then when the app detects the request for transfer it should popup a dialog with grant or deny. if granted (OnClick event) you should run a thread which when finished running will launch the activity where you can show the person what was received. – xaragen Sep 18 '12 at 15:14
  • Thanks.. Could give me some link for understanding how to implement Dialog box.. Thanks for spending time for me – Sai Krishna Sep 18 '12 at 15:16
  • http://www.mkyong.com/android/android-alert-dialog-example/ will help you get started with dialog and for threads or rather AsyncTask (threads but little better when it comes to android) Go here http://stackoverflow.com/questions/9671546/asynctask-android-example – xaragen Sep 18 '12 at 15:19