Start second activity after finish the first activity - android

I have developed an Android app, in which I have created 3 activities. When the app starts, the MainActivity launches the ImageActivity for playing images from the sdcard/Video folder of an android device.
And I have also a VideoActivity is for playing videos from the sdcard/Video. Means the images and videos are stored only in one folder in the called Video(in sdcard).
Now what I want, if ImageActivity starts to play the images then after finishing the images the VideoActivity should start to play the videos, and again ImageActivity should start then after VideoActivity then ImageActivity, and so on.
I have tried a lot for it, but it is not working. Searched for it on this site, but don't get helpful answer.

You can do this with passing intent with bundle from one activity to another at every single completion of activity. do the same thing in the second activity to start first one.
Intent i = new Intent(firstactivity.this,secondactivity.class);
Bundle b= new Bundle();
bundle.putStringArrayList("list",list); // Here you have to pass your list data
i.putExtras(bundle);
startActivity(i);

This is not a good approach.
It would be better if you use 2 fragments in one Activity. The activity loads the screen again and again but by using a fragment you can inflate those views only.
You should create two fragments. One for Image and the second for video.

Related

Re-Use the resources after finish()

I have several Activities in my application named, MainActivity, ChangeActivity and some others.
MainActivity displays some images from internet and refresh the page after a specific time. ChangeActivity will alter some data in MainActivity, so the images in Main page will be changed accordingly and on Submit will return back to MainActivity.
The application works fine, when it starts. But when I navigate from MainActivity to ChangeActivity, the previous MainActivity remains as it is and again a new Activity starts. As the previous MainActivity is still alive, it will try to auto refresh, eventually after repeating the same procedure for several times, it takes a huge time to load images in Main page.
I am looking for a solution, so I can destroy the previous MainActivities and have only have one, so the images from internet can download faster. Also other activities use the data from MainActivity. So destroying this completely might affect others.
Please do let me know, how I can utilize the resources effectively without affecting the other activities.
You can add finish() right after you start another activity. For example if you are starting activity ChangeActivity from MainActivity
Intent intent = new Intent(MainActivity.this, ChangeActivity.class);
startActivity(intent);
finish();

Android place activity on another activity

I have two activities in my app, FirstActivity and SecondActivity. FirstActivity accesses information from the internet so it takes a few seconds to load initially. I'd like to be able to open SecondActivity and then easily go back to FirstActivity without reloading it.
One of the thoughts I had on how best to accomplish this would be to open SecondActivity on top of FirstActivity, and then remove SecondActivity, when the user wishes to go back to FirstActivity. That way FirstActivity doesn't have to reload because it was loaded the whole time, merely hidden behind SecondActivity.
However I'm not really sure how to best accomplish this task. I couldn't figure out how to open an activity on top of another activity. Any help on how best to do this would be greatly appreciated, thanks!
FirstActivity accesses information from the internet so it takes a few seconds to load initially. I'd like to be able to open SecondActivity and then easily go back to FirstActivity without reloading it.
I recommend that you start with the 2nd activity directly, and from there you begin an async task to download whatever you need from the internet. When the async task is done, you simply switch to the first activity, which uses the things you downloaded. If you have few things to pass over, consider putting the downloaded info as an extra to the activity switch intent.
Alternatively, look into fragments:
http://developer.android.com/guide/components/fragments.html
Have your two current activities be two fragments. For the main activity, set the 2nd fragment as active at the start of the app. When the download is finished, switch to the 1st fragment.

How to stop loading data again while using intent to navigate activity?

I have three activities in my application ... I use an intent to navigate between the activities.
My third activity contains a method that fetch data from a web service.
When I press back button on the 3rd activity I go back to the second activity. But when I again come to 3rd activity from the second activity it starts downloading data again.
Please help me!
I am using handler in my 3rd screen..
Second activity uses following code
Intent intent=new Intent(Screen2.this,Screen3.class);
startActivity(intent);
Thanks in advance guys.
Try this link: Android Intents - Tutorial.
Using this tutorial
startActivityForResult();
Make use of savedInstance bundle....
Get more info from
Saving Android Activity state using Save Instance State
First finish the second activity moving to the third activity.
From third activity call the second activity

Refresh Activity Problem

I have an application with a list of video files with play button. When the user clicks play button, a separate activity starts through intent where the video is played. What I want is that when I click the back button when the video file finishes, I want the mainActivity to be refreshed (the mainActivity is the activity that started the activity for playing video file).
Any useful suggestions please??
Put your "refresh"-code in the Main-Activity's onStart-method.
See Androids Activity Lifecycle.
You could use startActivityForResult(Intent, int) and then handle the different results.

android: Moving through activities without popping them

I have the following I wish to achieve:
Activity A -> Activity B
Activity B -> Activity A
On paper it sounds like the same thing as just hitting the back button. Problem is, I don't want Activity B to be popped.
I need something along the lines of moveTaskToBackground but for a single activity. That way when I go back to it from A again, it can resume.
To give you some context, Activity A is a list of items. Activity B displays information about the item and plays a song attached to it. While going back from B to A, I want that song to keep playing, that way if the person hits the same option in A they just resume the activity. Otherwise kill that activity and start a new one with the new details and play the new song.
You need to look into ActivityGroup and LocalActivityManager. TabActivity (as suggested in other answer) actually extends ActivityGroup.
Here is a quick example to get you started:
Use this to start activity A:
LocalActivityManager manager = getLocalActivityManager();
//A_ID == 1
Window activityWindow = manager.startActivity(A_ID, intent_activityA);
setContentView(activityWindow.getDecorView());
or this to start activity B:
//B_ID == 2
Window activityWindow = manager.startActivity(B_ID, intent_activityB);
setContentView(activityWindow.getDecorView());
Activities are cached and associated with specified IDs. If you invoke startActivity with the same id several times, then cached activity is actually used (not always, depends on intents you use). You can easily remove any activity from cache if you need. Look into LocalActivityManager for more details.
You can use a TabActivity to switch between activities. You can do the switching within the code. Just hide the tabs in your UI.
Here's a quick tutorial on how to get started: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

Categories

Resources