I want to make a new layout for a menu item. For example the menu has the items: About and Exit. When I click the item About, I want it to go to a new page that shows the credits.
I have made something like this, but I get an error, any help?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.xsi_zero, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.aboutMenu:
setContentView(R.layout.about);
break;
case R.id.exitGame:
XsiZero.this.finish();
break;
}
return true;
}
I get the error on this line:
setContentView(R.layout.about);
If you want to start an Activity you do it with Intents. Intends are kind of messenger to carry our messages, requests in simply definition.
//This creates a new Intent object that has home and target
Intent niceIntent = new Intent(YourCurrentActivityName.this, TargetActivityName.class);
//Now start your Activity(Operation)
startActivity(niceIntent);
...
Above codes about our Home Activity also we must do somethings in our Target Activity.
In your Target activity's onCreate method AFTER super.onCreate.. you must set setContentView(R.layout.about); to see your about page.
Also another important issue is about AndroidManifest.xml
Android system must know every change. If you add a new Activity to your application you have to inform AndroidManifest.xml
How inform AndroidManifest.xml for new Activity?
<activity
android:name="com.yourpackagename.projectname"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Related
I have a problem that is annoying me. I have made an application that has two activitis. The first activity has a button and when you press the button it starts the second activity. I'm in the second activity and I press the home button to exit the app, and when I reopen the app the activity launched it's the first one. I want to launch the activity where the app was closed, in this case the second one. How can I do it?
public class FirstActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
View bub = findViewById(R.id.card_ub);
bub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(FisrtActivity.this, SecondActivity.class);
startActivity(intent1);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
}
return super.onOptionsItemSelected(item);
}
}
Second Activity -
public class SecondActivityActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
View bub = findViewById(R.id.card_ub);
bub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(FSecondActivity.this, FirstActivity.class);
startActivity(intent1);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
}
return super.onOptionsItemSelected(item);
}
}
Manifest file -
<application
android:allowBackup="true"
android:icon="#drawable/dc"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".FirstActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:label="Example"
android:screenOrientation="portrait"
android:theme="#style/AppTheme">
</activity>
</application>
</manifest>
Check developer page for better understanding of backstack.
http://developer.android.com/guide/components/tasks-and-back-stack.html
just call startactivity(new Intent(FirstActivity.this, SecondActivity.class). if you shoould not call finish() when starting new activity. if you are using intent flags like CLEAR_FLAG it will destroy your activity when starting the second activity.
Check your phone setting if you setup "Don't keep background process".
In your case, seems your phone system always kill your application when entering background.
So, if your phone always does that, you can do two things:
close this setting.
Or, you can save activity status in SecondActivity, e.g, onPause / onStop, to let your application know that your current status is SecondActivity.
Next time you open the application, check your application status in FirstActivity, and restore the status to SecondActivity.
I have an activity with the new Toolbar. In this toolbar i have only one icon... my SearchView icon. When i click on that icon it opens an EditText in the Toolbar and im able to write what im looking for. The problem is that when i click on Search icon, the content of my Activity (FrameLayout with fragment) is reloaded.
EDIT:
plus: when click on icon, it reloads the activity and open EditText, after that the Activity SearchResult is called, and if i press the back button in that activity i return to the MainActivity and the searchview is still opened.
How to prevent that?
Thats my Manifest part of search (MainActivity has the icon):
<meta-data
android:name="android.app.default_searchable"
android:value=".activity.SearchResult_" />
<activity
android:name=".activity.MainActivity_"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".activity.SearchResult_"
android:label="#string/title_activity_search_result"
android:screenOrientation="portrait" >
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
Thast my OnCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
{
public boolean onQueryTextChange(String newText)
{
Log.d("Query", newText);
return true;
}
public boolean onQueryTextSubmit(String query)
{
Log.d("Search", query);
SearchResult_.intent(MainActivity.this).extra("query", query).start();
return true;
}
};
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
I know I am late to the thread, but here is the proper solution. The way the SearchView default behavior works is that it will automatically send an ACTION.SEARCH intent to the searchable activity declared in your manifest. Upon sending, it will create a new instance of that activity, so that the onCreate() method can be called and the intent can be handled.
In order to work around this behavior, simply declare the launchMode for the searchable activity as 'SingleTop'. android:launchMode="singleTop"
According to Android documentation for "singleTop"
(https://developer.android.com/guide/components/activities/tasks-and-back-stack):
"If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity." If you declare your searchable activity as 'singleTop', make sure you override onNewIntent() and handle the ACTION.SEARCH intent data accordingly.
In your onQueryTextSubmit method do:
searchView.setQuery (query, false);
You cant. unfortunately everyone has the same exact issue. searchViews always call the query when it is opened. no way to avoid it. I have seen some people have a boolean called first and set it to true. then only do logic if it is false. then on the first query call set it to false. not a good way but it should work
I don't know if that it is a bad practice, but I fix it like this:
#Override
protected void onResume() {
super.onResume();
if (searchView != null) {
if (searchView.getQuery().length() <= 0) {
searchView.setQuery("", false);
}
}
}
I have an Activity name "IntroActivity" that is the "Launcher" activity for the app and after some decision making opens up a new Activity named "HomeActivity".
In my HomeActivity I have an ActionBar with a "Log Out" Action (which is in the menu but set to appear as an Action) which is supposed to do some operations and return the user to the "IntroActivity".
However, the startActivity method used inside HomeActivity does not work and there are no errors generated. I have debugged the code and the onOptionsItemSelected method is definitely executed as is the correct case inside. Here is the code:
HomeActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_standard, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_standard_settings:{
// TODO
return true;
}
case R.id.menu_standard_logout:{
//Set logged in to false and then return the user to the intro page
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("logged_in", false);
Intent introIntent = new Intent(this, IntroActivity.class);
startActivity(introIntent);
return true;
}
}
return super.onOptionsItemSelected(item);
}
The Manifest includes:
<activity
android:name="com.kennel39.diabeteslive_adtdev.IntroActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I'm not sure what else I can provide that will be of use but I will quickly post up any extra parts if required.
Thanks in advance,
Josh
I think you miss commit the shared preference after setting loggedIn as false. So your flag is not updated and your intro activity again redirect you to home activity.
Try
editor.commit();
From what I can see, you miss two things to make things work:
Add editor.commit(); just after editor.putBoolean("logged_in", false); in order to save your value "logged_in"
Add finish(); between startActivity(introIntent); and return true;
I have my simple PreferenceActivity class and its onCreate passes my R.xml.preferences screen to ((PreferenceActivity)super).addPreferencesFromResource. Finally, in my AndroidManifest.xml my activity as follows:
<activity android:name="com.criticalrf.jwalkietalkie.PreferenceServerActivity" >
<intent-filter>
<action android:name="android.intent.action.MANAGE_NETWORK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
On the device, the menu button will trigger a menu with only the word "Settings." Clicking on Settings makes the Settings button go away but does not show anything. Do I need to add something in my MainActivity? I followed this guide, I'm not sure what I missed.
If you want to open this preference activity from your main activity you should call somewhere in your MainActivity:
startActivity(new Intent(this, PreferenceServerActivity.class));
and preference activity will show.
Typical implementation would be doing it with options menu. Like:
private static final int SettingsId = 1;
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, SettingsId, 0, "Settings");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case SettingsId:
startActivity(new Intent(this, PreferenceServerActivity.class));
return true;
default:
return false;
}
}
Intent filter in your manifest from tutorial just says, that this activity can be opened from system settings when user browse data usage of your app.
I created a base activity that has a menu, this menu will open other activities in the application.
public class BaseActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Intent i = new Intent("com.app.Activity1");
//startActivity(i);
}
#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.icon: Intent i1 = new Intent("com.app.Activity1");
startActivity(i1);
break;
case R.id.text: Intent i2 = new Intent("com.app.Activity2");
startActivity(i2);
break;
case R.id.icontext: Intent i3 = new Intent("com.app.Activity3");
startActivity(i2);
break;
}
return true;
}
}
All activities extend this base activity, so when you press the menu button, the menu pops up and you can select an activity.
However, lets say I use the menu to go to activity A. Once I'm in activity A I can use the menu to go to Activity A again. I can do this X times, but now the back button will go back to the same activity X times.
How can I tell if the activity is already running so a user can't keep opening the same activity?
Or rather, would you suggest I disable the menu item once in activity A?
Thanks for your input. Sorry if this seems like a trivial question.
There is a simple solution to this. You want to disable the menu item for the current activity in onPrepareOptionsMenu() in your BaseActivity like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Determine what activity I am and find the menu item for that activity
MenuItem menuItem = null;
if (getClass().equals(com.app.Activity1.class)) {
menuItem = menu.findItem(R.id.icon);
} else if (getClass().equals(com.app.Activity2.class)) {
menuItem = menu.findItem(R.id.text);
} else if (getClass().equals(com.app.Activity3.class)) {
menuItem = menu.findItem(R.id.icontext);
}
// Disable this menu item
if (menuItem != null) {
menuItem.setEnabled(false); // Make it non-selectable (even with shortcut)
menuItem.setVisible(false); // Make it non-visible
}
return true;
}
I think u need not to do that.I think you just appoint your activity's launchMode="singleTask".Code in your AndroidManifest.xml:
<activity android:name=".com.app.Activity1" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait"></activity>
<activity android:name=".com.app.Activity2" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait"></activity>
<activity android:name=".com.app.Activity3" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait"></activity>
^-^
Kind of a trivial solution - why not have a global int in your baseActivity called 'int choice'? When you switch to an activity just set choice to that activity. For example
case R.id.icon:
if (choice != 1){
startActivity(i1);
choice = 1;
}
break;
You can implement this in two ways:
To call activity A from activity A, call it with flag as FLAG_ACTIVITY_REORDER_TO_FRONT, so A will be called again without creating a new instance, so goin back will not create any problem.
As you said, disable the menu item for activity A if activity A is in front.