In My Android app the flow looks like this: Main Page -> Page1 -> page2 -> page3. In Page1, Page2 & Page3 am having Home button, when clicked on Home button, It should launch Main Page(MainActivity). Here MainActivity Class gets the JSON Data from the server.
Now my question is: when am in Page1, Page2 Or Page3, if i click on Home button it should go to MainACtivity.At the same time, When Am in Page2, if i click device back button it should go to Page1. Simillarly, If am on page3, If i click on device back button it should go to page2.
For this, The usual solution i have used like,
//In Page1 Activity, Under Home button OnClickListner:
Intent in = new Intent(Page1.this,MainActivity.Class);
StartActivity(in);
With this, Everytime the MainActivity Class gets load & Communicates with Server, that results low performance in app.
Another Approach i tried i.e,
//In Page1 Activity, Under Home button OnClickListner
finish();
This works fine for page1. If i use the finish() in the Page2, it launches the page1 activity. Alternatively i have used somewhat like this:
//In Page1 Activity:
Intent in = new Intent(Page1.this,Page2.Class);
startActivity(in);
finish();
And, in Page2, under Home Button OnClickListener i have written finish(). With this, when am in Page2 if i click on Home button, it will launch the MainActivity. At the same time if i click back button of device, it is launching MainActivity. It has to go to Page1.
Please, Can someone suggest me what i need to use for this suitation.
Thank you All.
You should set a flag in your intent in the child activities.
Intent i = new Intent(Page3.this, MainActivity.class);
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
http://developer.android.com/guide/components/tasks-and-back-stack.html, for more information.
Use This code:
in the OnClick of Home button in all the page, write this:
Intent in = new Intent(Page1.this,MainActivity.Class);
startActivity(in);
finish();
in onBackPressed of page1, write:
Intent in = new Intent(Page1.this,MainActivity.Class);
startActivity(in);
finish();
in onBackPressed of page2, write:
Intent in = new Intent(Page2.this,Page1.Class);
startActivity(in);
finish();
in onBackPressed of page3, write:
Intent in = new Intent(Page3.this,Page2.Class);
startActivity(in);
finish();
Try adding a flag to your intent before tarting the activity like this.
Intent mIntent = new Intent(Present_Activity.this, Next_Activity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
EDITED
And in your main Activity before you call finish make a call to method:
moveTaskToBack(true);
Somehow i got the solution to my suitation..
On Page2, Home button SetonClickListener have written code like this:
Intent in = new Intent(Page2.this,MainActivity.class);
startActivity(in);
Under Manifest file:
For Main Activity have used like this
android:launchMode="singleTask"
with this, Everytime click on Home button launches the instance MainActivity but doesnt communicate with servers.
Have a look at docs , you can use android:parentActivityName attribute in manifest file while declaring any activity.
Example:
<activity
android:name="com.example.Page2"
android:parentActivityName="com.example.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
You don't need to start MainActivity using Intent.It will handle to call ParentActivity while executing onBackPressed.
If you need to add SupportLibrary , you can download it from here.
EDIT :
you should manage Activity Lifecycle as mentioned here.
Inside MainActivity's onCreate method, initiate execution of AsyncTask to load data from server.
and when you move back from Page2 , onResume of MainActivity will be called. so your data will not be loaded from Server again.
I hope it will be helpful !!
Related
I'm using this library which is a Wizard.
When user is done with the Wizard and clicks Done button he moves to another Activity.
I have added to Manifest file android:noHistory="true" for the WizardActivity and also when I start the next activity im doing it like this :
Intent intent = new Intent(this, ProjectsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Nevertheless when user click backs he still goes to the WizardActivity. I'd like the user either to exit or go the activity that started the WizardActivity.
Thank you
finish() the activity after launching the other Activity. i.e after startActivity(intent) add finish()
In my app I have two fragment in ManiActivity.class .
In my fragment I have a listview , when I click a item in list I open MainActivity2.class . In MainActivity2.class I have listview related and when i click list related I open MainActivity2.class with new value. Now I want create a button back home in MainActivity2.class and when I open many MainAcitivty2.class when click list related on it I can click button back home to go to MainActivity2.class
I try it with
Intent intent = new Intent(MainActivity2.this,MainActivity.class)
startIntent(intent)
But when I click back button in MainActivity.class it comback MainActivity2.class , it not exit app.
How I do to clear it?
Please help me.
This is why Android maintains states of each activity in a Stack, that's why when you will press that Back-button, you just have to clear all previous activities from the stack and then open MainActivity
Just you have to set flags before starting intent:
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Use android:launchMode="singleTask" for your MainActivity in AndroidManifest.xml.
Which will keep only one instance of MainActivity if launched again this activity will come to top of the stack.
And Use this code
Intent intent = new Intent( MainActvity2.this, MyActivity.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
current_activity.startActivity( intent );
call finish method after startIntent
something like this:
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
startIntent(intent);
finish();
and put finish method in your activity
#Override
public void finish() {
super.finish();
}
its very simple.. use finish();
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
startIntent(intent);
finish();
bcz u create multi activity in mainActivity2. so u have to destroy your activity before u go to mainActivty.
otherwise when u open new many in mainactivity2. that time use finish.. bcz u must have to destroy first your mainActivity2.
if you not understand my answer then just post your whole code... i will edit...
I have three activities connected witdh three layouts. When I call an activity everytime before one collect background. eg. I am on the mainactivity then, I call B then call main again when I am on B, after that I call B and this sitution if I click back on screen B, it comes B>main>B>main or something like such as B>A>main>A>main>B>main...
private Intent intent_Main;
.
.
.
intent_Main = new Intent(this, MainActivity.class);
startActivity(intent_Main);
This is my code for calling activities, for B (this, B.class) like that...
I want when I click back on different from main it just turn main and when main shows it exit -not show previously activity screens-...
Edit:
How can I close all screen (exit) on main activity and how can I return just main activity from others?
[If I go to B.class and its screen then go to main activity (with button etc.) and then again B, after that I click back I return Main activity then push again return first B and then click back it returns first time openin mainactivity after that it close]
Instead of starting a new Intent to go to your Activty Main each time you're in your Activity B just call finish()
use
main.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
or in manifest use
android:launchmode="singleTop"
You should call:
finish();
If you don't want to have the user return to an activity after starting a new activity using startActivity().
The following code starts a new activity and then calls finish() to exit the current activity:
Context ctx = getApplicationContext();
Intent intent = new Intent( ctx, MainActivity.class );
// Start our real activity
startActivity( intent );
// Finish this activity so user doesn't return via back button
finish();
Having real issue understanding how to sort my issue out.
On the Home screen I have 2 buttons
When the user clicks the first button it starts a new Activity. What I am looking for is if the user clicks back the app returns to the home screen. If the user clicks the first button again it starts a new activity.
If the user clicks the second button it returns to the activity that was last started by clicking button 1
What I am having issue with is how to save the state of the activity when the user clicks back
Also how to call that activity when the second button is pressed
Thanks for your Time
UPDATE
I have gone down part of this but still having issues. If I put some of the code I am using perhaps someone can point where I gone wrong.
Code for calling the new activity from main menu
Intent intent = new Intent(MainMenu.this, NewClass.class);
intent.putExtra("value1", value1);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Within the new class I have added the following :
#Override
public void onBackPressed() {
//super.onBackPressed();
Intent intent = new Intent(RoundScoring.this, MainMenu.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Toast.makeText(this, "Back Button Pressed", Toast.LENGTH_LONG).show();
}
I do not have either a onrestoreinstancestate or onresumne in this class. only a oncreate. Do I have to add something like this to bring back the instance
On the second button on the main menu I have added this
Intent intentContiune = new Intent(MainMenu.this, NewClass.class);
intentContiune.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentContiune);
Thanks
Try this:
Home Screen Left Button:
Open the new activity with an intent flag, FLAG_ACTIVITY_NEW_TASK
Activity:
Override onBackClick() on the started activity to call the home screen with an Intent instead of finishing it. Use the flag FLAG_ACTIVITY_REORDER_TO_FRONT
Save activity state overriding OnSaveInstanceState
Home Screen Right Button:
Call the activity with the flag FLAG_ACTIVITY_SINGLE_TOP
More info about flags:
http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/
One solution may involve passing bundles around that include the state of your activity. Using startActivityForResult(), you can return a bundle with the activity's state. When the user clicks your second button, pass in that bundle and have the activity set itself up with respect to the contents of the bundle. If the bundle doesn't contain the information you're looking for, then use the default values as if you were just starting it.
For more information:
Android: Capturing the return of an activity
I have this Activities sequence:
Avtivity01 => Avtivity02 => Avtivity03
You can go from Avtivity01 to Avtivity02 after you click a button.
You can go from Avtivity02 to Avtivity03 after you click a button.
-
I want to go from Activity03 to Activity01 DIRECTLY after I click a button.
-
NOTE:
I do NOT want to use Intent, because I want to have Activity01 as if I pressed the back button from Activity02
How to do that, please?
Why can't you use an Intent? You can use the FLAG_ACTIVITY_CLEAR_TOP when you click the button. Edit: If you want to preserve the original instance of the Activity, you can use this flag in conjunction with FLAG_ACTIVITY_SINGLE_TOP.
Do you ever want to be able to press a button from Activity03 to go back to Activity02? If you ALWAYS want it to go back to Activity01, you could alternatively use android:noHistory="true" on Activity02 in the manifest and just call finish() on Activity03.
You can go back more than one screen to which ever screen you need using intent and use flags to prevent going back to same screen :
Intent gotoScreenVar = new Intent(goFromScreenCls.this, goToScreenCls.class);
gotoScreenVar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(gotoScreenVar);
I was had the same problem,
Short answer is add this line to intent.
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Long answer is:
I have MainActivity, SecondActivity and ThirdActivity what i want to do is opening SecondActivity from MainActivity and then ThirdActivity from SecondActivity and i want to click a button in the ThirdActivity that close both of SecondActivity and ThirdActivity without calling a new MainActivity i want it to functional as i back to it not creating a new one And the most thing i need is to keep back button functional normal in the ThirdActivity to back to SecondActivity if i need to change some of my choices in the SecondActivity so i can't call finish(); in the SecondActivity after starting ThirdActivity or any other solutions like android:noHistory="true" in the SecondActivity manifest because those solutions kill the SecondActivity and i can't go back to it throw back button.
so this flag solve the problem for me FLAG_ACTIVITY_REORDER_TO_FRONT
FLAG_ACTIVITY_REORDER_TO_FRONT
If set in an Intent passed to Context.startActivity(), this flag will cause the >launched activity to be brought to the front of its task's history stack if it >is already running.
go to reference here
so i tried this code to go back my MainActivity from ThirdActivity when i click the button in ThirdActivity . i hope this will help you.
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
link has good article. This will help you. By the way you can use FLAG_ACTIVITY_REORDER_TO_FRONT to solve your problem.
You can override Back button click on Activity3
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
startActivity(Activity3.this,Activity1.class);
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
It will work with just one line.
after calling your third activity call finish() to next line. and it will work.
eg.
In your Activity02
Intent thirdActivity = new Intent(this,Activity03.class);
startActivity(thirdActivity);
finish();