My problem is almost same like
Similar Problem
But the solution didn't work for me.
In my two different app I am using same menu class file but they are showing different result. I have set menu in which clicking one of the items directs toward "AboutActivity" . When in one app I back from the menu's "AboutActivity" it refreshes the "MainActivity" but in another doesn't. The difference between the two apps is first one has two activity's (Main+About) and second one has five activity's (Main+About+Main2+Main3+..)
The code I have used in the Manifest.
<activity android:name="school.infinity.maruf.agecalculator.about"
android:label="About"
android:parentActivityName="school.infinity.maruf.agecalculator.MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|layoutDirection|touchscreen"/>
This is the code used in class file
public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
switch (item.getItemId()) {
case R.id.about:
Intent intent=new Intent(this,about.class);
startActivity(intent);
return true;
}
I have used the following code for MainActivity in the manifest
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|layoutDirection|touchscreen"/>
Now how can I stop reloading/refreshing the main activity during getting back from "AboutLayout"?
Related
I made a app with multiple activities. And there are some problem that I cannot solve on my own.
For example, I have 4 activities named "MainMenu","Secondpage","Thirdpage", and a "Fourthpage". From The MainMenu , I pass a id with putExtras() to pass over to the Secondpage so that the Secondpage gets the id and sets it into a url to get the json data from the web. After the "Secondpage" gets the json data,it will display it in a listView. The user will now be able to click the listView to mover over to the "Thirdpage" that contains the id and the data passed from the "Secondpage". Now the problem is that from this "Thirdpage", I want to go back to the "Secondpage" but I cannot find a way to do this.
I've set the manifest file like the follwing, however, I'm not sure if I'm doing it right. Or should I set something up in the ActionbarActivity class?
Sample
<activity
android:name=".Secondpage"
android:label="#string/title_activity_first_time_user"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Thirdpage"
android:label="#string/title"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.me.Secondpage" />
When you say
I want to go back to the "Secondpage"
you mean like if you have pressed the back button?
If so, you can just call finish(); in your third activity to do that.
EDIT:
In your third Activity, add
getActionBar().setDisplayHomeAsUpEnabled(true);
or, if you are using the support library
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate(Bundle b) method
And
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
elsewhere in your class.
Just call
onBackPressed()
if you want to have a behaviour like the user uses his back button. You should return to the SecondPage without any problems.
EDIT:
If you want to use the back button in the action bar check this one out.
I've been researching all day/night for a solution, but it seems there are lots of options to go from an Activity to a Fragment, but none are working for me on S.O. In practice, I am in an Activity, and I want to use my app logo in the ActionBar to click it and then return to a Fragment. This Fragment is the "parent class" of my Activity, meaning there was a button in the Fragment I clicked that took me to my Activity.
But I can't get all the code snippets I've seen to work.
I have put this in my onCreate() of my Activity:
// Shows the up carat near app icon in ActionBar
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I also included this in my onOptionsItemSelected() method of my Activity:
// Handle action bar actions click
switch (item.getItemId()) {
case android.R.id.home:
android.app.FragmentManager fm= getFragmentManager();
fm.popBackStack();
return true;
default:
return super.onOptionsItemSelected(item);
}
The result is that I see a "back button" carat (as shown below), but when I click it nothing happens. I'm supposed to go back to the Fragment I came from. FYI, my Fragment class actually extends Fragment (not FragmentActivity). My Activity extends ActionBarActivity, so I am looking for an answer that will work for Android 4.0+. Also, my Fragment does not need the same instance (necessarily) when it is returned to. It only has buttons on there, so a new instance is fine, if it gets created, upon returning.
Thanks for your help!!
One small line was needed: finish(). Since the FragmentManager pops off one item in its backstack, by using fm.popBackStack();, it still needs some sort of action to go to the previous fragment. Adding finish() enabled the current Activity to end. The line in context:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
case android.R.id.home:
android.app.FragmentManager fm= getFragmentManager();
fm.popBackStack();
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The rest of the code I had above was correct and is needed too, to make it all work. Now, I am able to navigate to my NavigationDrawer fragment, click a button there to go to an Activity, then press the navigation UP caret to return to my Fragment at anytime. This was tested successfully on a Samsung Galaxy5 phone.
One thing that you do not read about in the Navigating Up with the App Icon section of the ActionBar Android doc is that since you are using a fragment to return to, you cannot use their <meta-data> tag instruction to specify the parent activity in the manifest file, because you are not returning to an Activity! But rather a Fragment. So a workaround had to be achieved by using FragmentManager.
I am using a Navigation Drawer on the main activity of my app with three options. Option one is the default fragment being used in the main activity. Clicking option two just replaces the old fragment with a new, but stays in the same main activity. This is all good.
Next step is that I click one of the list items in option two and it opens a new activity+fragment. I implemented the Home Up button in the new fragment with
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The problem comes up with I hit the Home Up button. It takes me to the main activity with the initial fragment (option one) and not the option two fragment. How do I change this?
You need to override onOptionsItemSelected in Activity two and just finish it when home pressed
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I'm currently moving slowly but steady forward in the making of a Android application and Im currently learning how to create a new window and switch to it. This is going all well but I have one small problem. When I pressing the "go back" button closes the application even if I have choosed to go back when just that button is pressed.
#Override
public void onBackPressed() {
finish();
return;
}
Have I missed something or what?
Thanks in advance.
EDIT
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.menuItem1:
setContentView(R.layout.about);
return true;
case R.id.menuItem2:
System.exit(0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EDIT 2: About.java
package weather.right;
import weather.right.now.R;
import android.os.Bundle;
public interface About {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
You need to use Intents to "switch windows". A "window" is called an Activity in Android and you set the visual content of an Activity using the setContentView() method in the onCreate() call of the Activity.
To launch another Activity from your current Activity, simply create an Intent with a few parameters and call startActivity() with that Intent. Here's an example:
Intent i = new Intent(this, TheNextActivity.class);
startActivity(i);
Don't forget to include your second Activity in the Android manifest file. All Activities in your application must be included in that file.
Other things to note is that you don't really use System.exit() in Android. Just call finish(). It's advised to let Android manage applications and its resources rather than doing it yourself, but if you want to make sure that your application really is shut down, feel free to use System.exit() anyway. There's also no need for overriding onBackPressed() if you're only calling finish(). That's standard behaviour in Android when you hit the back button.
Also, you don't call setContentView() more than once per Activity. You start a new Activity when you need to change the visuals (or use one of the specialized Widgets to switch between layouts.
This also explains why you're experiencing your "problem". You may have changed the layout of the Activity using setContentView(), but there's still only one Activity running - when you call finish(), that Activity gets closed. If you had started a second Activity with a different layout, like you're supposed to do, Android would have closed that second Activity and would have returned you to the first.
I am writing my first Android app. It's a port of an iPhone app which has 3 tabs at the bottom, call these A, B and C. Tab A has 4 child activities (A1, A2, A3, A4) , tab B has 3 child activities and Tab C has 2 child activities.
For the Android app, I don't really want to show the iPhone style tab bar at the bottom. In fact I'd rather have no tab bar at all (so I can use more of the screen) and instead use the Menu button to swap between activities A, B and C.
I'm really struggling to choose the best method to implement this which will also need to handle the back button correctly.
I've read the Android developer notes on Activities and tried this out (using the intent flags to disable the 'slide left' effect) This works for switching between A1, B1, C1, but if you've navigated A1, A2, then C1 i don't know how to make the Back button go to A2 (it goes to A1)
I've also read about using Tabs and tried a test with this. However the back button is not handled and simply exits the app. I realise I can handle the Stack myself and override the Back button and that I'll need to use ActivityGroups. But I've not found any good examples of how to handle the back button in the ActivityGroup and also read that theres a bug in handling the back button if one of the Activities in the Group is a ListActivity, which I also intend to use.
Any help or pointers would be appreciated to help get me started.
If you use TabHost it's quite hard thing to handle and I wouldn't recommend that
If you use Context Menu than calling each activity normally (!not withing activity group) should put the Activity to the Activity Stack and make standard functioning of Back button available.
Alternatively you can imitate bottom menu just by a separate menu View which you include to all the layouts which will avoid TabHost limitation but will give the same look.
This is how I go about using the menu button. In your main activity have something like this:
First, make a new android xml file and set the type to menu (its one of the dropdowns in eclipse), then the menu.xml file will automatically be made in the right place.
Open up the layout, and add your 3 items (one for each page you want to switch to)
// Menu button pressed
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.page1:
startActivity(new Intent(this, page1Activity.class));
break;
case R.id.page2:
startActivity(new Intent(this, page2Activity.class));
break;
case R.id.page3:
startActivity(new Intent(this, page3Activity.class));
break;
}
return true;
Where page1Activity and the rest are the other classes.
Make sure that in your Id section of the layout you have #+id/page1, etc...
Just add those activities to your manifest and it should work. The back button works fine with this (though I havent tried with a listview. With a listview you should just have a finish(); when you select something to avoid multiple lists)
To switch subpages use something like this:
public void page1aPressed(View button) {
Intent nextPage = new Intent();
nextPage.setClassName("com.example",
"com.example.page1a");
startActivity(nextPage);
When you press back it just goes to the previous activity. Maybe I'm thinking of this wrong but it works for me.