As I create navigation activity I always use fragments.
But now I want to use activity instead of that.
Is it possible to do so?
Sure you can,
just start the new activity with an Intent :
Intent i = new Intent(getApplicationContext(), SecondScreen.class);
StartActivity(i);
Related
I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();
For eg: I am using one Activity for Different UI pages with different he. So I want to check if an activity with a particular parameter in the intent exists. If it does just bring it to the top or else create another instance of an activity.
I have tried adding the flag FLAG_ACTIVITY_CLEAR_TOP to the intent but that does not solve my purpose.Here is some code: I want to create an activity only if there is a new title.
Intent intent = new Intent(activity, MyActivity.class);
intent.putExtra(DISPLAY_NAME, title);
activity.startActivity(intent);
I an have activity called A in my project, there is a button that create new instance of the same activity. For example i want to do something like this:
intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(MainActivity.EXTRA_DATA, data);
startActivity(intent);
If i use this code the app crashes when i push the button.
I have found the solution. The activity use a object of another class called data, this class need to implement Serializable.
I have a little question which is bothering me. How can I finish activity C and start it's parent. But the tricky part is that I can start activity C from 20 other activites. The idea is to open the right one when i call finish on C. And the other thing is that I have tabhost , which childs opens activity C.
Any suggestions how to achieve this?
in your activity C save the following variable:
Class parent = ParentActivityClass.class;
override:
public void onBackPressed(){
//create an intent like
Intent i = new Intent(this, parent);
startActivity(i);
//add extras to intent if needed
this.finish();
}
please note that this might create a NEW parent activity. it is up to you handle this situation if this might create problems.
An alternate solution is to finish each other child activity when you launch a new activity. This will assure that on your stack you will have always the parent below the child activity.
I simply did something like this :
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
This did the trick.
Depend on your activity stack if your current exactly on top of the parent activity you can just finish current actvity and it will go to previous activity. If you want to clear all activity stack and start over new activity try
Intent intent1 = new Intent(context, activity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
which clear your stack and start over new if you have stack of activity over parent want to finish all use this and start over parent again.
Take a look at Task and Backstack, Implementing Temporal and Ancestral Navigation
There are more specific explanations. Hope this help
I Mean that i want to use one activity into another activity, Like class using create instance of that class. Is it Possible?
Well, I think you should use Intents to call an activity from another activity.
Call this from your Activity:
Intent in = new Intent(getApplicationContext(), NextActivity.class);
startActivity(in);
you can do it only by saying startActivity(), no other go. you can't make an instance of Activity because , an Activity gets created when its onCreate() method gets called, but when you say new MyActivity() its default constructor is called and not its onCreate() method (which Android OS will not accept). so always say startActivity() or startActivityForResult() which are handled by android OS
Write this code from where you want to run activity
Intent intent = new Intent(current_Activity_name.this,New_Activity_name.class);
startActivity(intent);
And add the following code into manifest file
<activity android:name=".New_activity_name" />
Well, since an Activity is a displayable-window, the appropriate concept would be that one Activity can be "launched" from another. This is how you achieve that:
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
CurrentActivity.this.startActivity(i);
This code snippet can launch NewActivity from any point in the CurrentActivity code, for example, an 'OnClickListener'.
Yes, it is possible. This is achieved through Intents.
Intent intent = new Intent(this.getApplication(), TARGET_ACTIVITY_NAME.class);
//To add data use intent.putExtra(NAME,VALUE);
intent.setData(data.getData());
try
{
startActivity(intent); // This ll launch the TARGET_ACTIVITY_NAME
}
catch(Exception e)
{
}
For more information refer this link.
Shash