How to navigate to another page in android? - android

I'm new to android. Please tell me how to navigate to a new page in android.
Thanks in advance.
Edit:How to start a new activity from an existing activity

In android to navigate to another page means you have to start another activity. for starting a new activity use this
Intent intent = new Intent(currentActivity.this, nextActivity.class);
startActivity(intent);
You should mention the next activity in the AndroidManifest file.

To change your page (i think you're refering to "Activities" in android you need to issue a so called "intent").
You cann do that by:
startActivity(new Intent(nextactivity,NextActivity.class));
or
startActivityForResult(new Intent(nextactivity,NextActivity.class),1);
if you want the new Activity to return any result to the activity who started the new one.
But what i recommend is, that you read this documentation here:
http://developer.android.com/guide/topics/intents/intents-filters.html
and come back to this question if you have need additional assistance on concrete problems on that topics.
i hope that helps.

Related

Using life cycles in android

I'm a new android programming beginner :) .
I have made a simple app which adding places .
1- Main activity include buttons{Places , Add place , Favorites Places }
so let's assume we are in the "Add place" activity , we type tite and description and add the place to arrayList, and move on to Places which has this list ..
The problem is when I type back, I get back the details I wrote about the place, but I'm trying to clear it when I press back in the phone ...
how can I do this to avoid crashes ?
Thanks all
-- Update :
Thanks to all people who reply trying to help me, that's awesome :)
I just added :
#Override
public void onBackPressed(){
Intent intent = new Intent(PlacesActivity.this,MainActivity.class);
startActivity(intent);
}
This solution was suggested by : Arsen Sench :)
in the activity that I don't want to press back,
You can do one thing here.
Before jumping to another activity View Places , just clear all the edittext there.
edittext.setText("");
Try this. Will help you.
just clear the EditText field before you start a new activity
editTextTitle.setText("");
editTextDescription.setText("");
intent.startActivity();
You can use startActivity() from both classes. Or you can use startActivityForResult(). Here is example of using startActivity() from both activites.
From first Activity
Intent i = new Intent(YOURFIRSTACTIVITY.this,YOURSECONDACTIVITY.class);
startactivity(i);
finish();
From Second Activity
#Override
public void onBackPressed()
{
Intent i = new Intent(YOURSECONDACTIVITY.this,YOURFIRSTACTIVITY.class);
startactivity(i);
finish();
}

Go back to previous screen without creating new instance

As explained in image, flow is something like this. So whenever user click on logo button Activity A should be called. As simple solution we can use this method...
Intent intent = new Intent(activity, activityToStart);
startActivity(intent);
But this activity will create a new activity for my app. but I need to call the same instance of the activity as we move forward in flow diagram. from Activity A to B and then again on B can be called easily by callingfinish() but from Activity C or D, how to come back to A.
I am running out of ideas but not getting any fruitful result. Please help me if you have any suggestion or at any place i am going wrong. Thanks in advance.
To Come Back from D to A, use Intent Flags.
Intent intent = new Intent(activity, activityToStart);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent);
FLAG_ACTIVITY_CLEAR_TOP, will instead of creating new activity, it will invoke the activity on the stack, and will pop all the activities over the activity being invoked.
Instead of Using
Intent intent = new Intent(activity, activityToStart);
startActivity(intent);
Use
Intent intent = new Intent(activity, activityToStart);
startActivityforResult(intent,1234);
This will make Sure that The Activity A is not Killed and when You finish Your Activity C,Activity A will get Resumed.
Note :- Whenever You create A new Activity,without finishing(Exiting) the Host Activity,The Host Activity is Saved On the Stack in LIFO order
LIFO:- Last In First Out
By making Activity A a "SingleTask" you can achive this. When a Activity is in the SingleTask on clicking the Home button the other activites will be removed from the stack.
Refer these link's for more info on Android Activites...
Link 1 - Android Fundamentals
Link 2 - Another Similar Question
Use ViewFlipper to go and back between different window in the same activity.
ViewFlipper vf = (ViewFlipper) findViewById( R.id.view_flipper);
To go to the next window
vf.showNext();
To go to the previous window
vf.showPrevious();
I am not sure,this is right way or not,but you can give it a try!
You can finish() current activity when you open the new one starting from Activity-B.
i.e.
To open Activity-C => finish Activity-B and start Activity-C
To open Activity-D => finish Activity-C and start Activity-D
now when you will press back,Activity-A will open.

Finishing Multiple Activity with Single Statement?

In android finish(); is used to close the Current activity. In my application i've more than 4 Activities. I want to finish them with one single statement. How can i done this here.
I've tried the System.exit(0); it's not working for me. Why this not working for me also? Anyone guide me here? Thanks in Advance.
Its better to always go back to homeActivity by using startActivity of your home screen. you can find answers here. How to close all the activities of my application? and Is quitting an application frowned upon?. its already discussed here
Use following set of instructions.
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I've got the answer. I've give the moveTaskToBack(true); to my Button Click event. This works fine. Thanks for you all whose reply me and answer me.

Android - start multiple activities

is it possible to start multiple activities at once? I mean, from main create 3 activities in some order and just the last will be visible? Up to now, I was able to create only one activity.
Thanks
You might need something like this in order to launch deep into the app after the user has clicked a notification in order to display some newly added content, for example.
Intent i = new Intent(this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
Intent j = new Intent(this, B.class);
j.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(j);
Intent k = new Intent(this, C.class);
startActivity(k);
In this way you can start activities A, B and C at the same time and suppress transitions to activities A and B. You get a single transition from your current activity to activity C. I strongly suggest that you log the Activity lifecycle method calls (onCreate etc.) to LogCat, for example. It helps a lot in understanding the order of events.
This can be a common thing to do in response to deep linking or other use cases where you, basically, need to synthetically rebuild the Task (and all the activities it should contain). Sometimes, just specifying parents in the manifest isn't enough.
Take a look at TaskStackBuilder. One common example:
TaskStackBuilder.create( context )
.addNextIntent( intentOnBottom )
// use this method if you want "intentOnTop" to have it's parent chain of activities added to the stack. Otherwise, more "addNextIntent" calls will do.
.addNextIntentWithParentStack( intentOnTop )
.startActivities();
Really old question but I thought I still answer it.
Use:
public void startActivities (Intent[] intents, Bundle options)
Try startActivity(new Intent(...); at the end of your onCreate-Method of the first Activity.
This will immediatly launch a new Activity and pause the first one.
With back-key you will get back to the last Activity

how to do something like multiple views ..stack in iOS on Android?

I was wondering about the following situation:
in iOS:
I have viewA which goes to viewB on button press. from view B based on some computations, I can either go to viewC or go back to viewA.
I have done this nicely on iOS.
I am wondering how to do the same in Android. Can anyone kindly help me out ? Thanks.
You mean the Task stack on android. Take a look to this, it will be what you're looking for.
For example (I've encapsulate this in a Navigation Manager instance, that handles all my navigation logic, but given your context, something like this could be useful:
Intent intent = new Intent(this.activityC, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //THIS IS THE TASK MANAGEMENT
if (!params.isEmpty())
intent.putExtras(params);
this.activity.startActivity(intent);
The Task Stack in Android is correct. If you need to specifically know how to move from one activity to another then just use intents like this to change to a new activity:
Intent startActivity = new Intent(view.getContext(), TargetActivity.class);
startActivityForResult(startActivity, 0);

Categories

Resources