startActivity is not starting my Activity in Android - android

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;

Related

Android App Activities Lifecycle

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.

PreferenceActivity not associated with "Settings"

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.

New page in menu

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>

Android: How to determine if an activity is already running?

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.

How to call Activity from a menu item in Android?

I'm attempting to call startActivity(myIntent) from the click of a menu button but my application crashes at that point.
The same startActivity call works fine from a regular button click, so, I assume the menu button is missing information about the context? Or maybe I'm totally off the mark here.
So... what's the correct way to have a menu item take me to a specific Activity?
I've revised my code based on the initial set of advice. Still crashing in the same place. The debugger doesn't enter the exception clause, the app just dies.
[EDITED WITH CODE SNIPPET]
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
try{
switch (item.getItemId()) {
case R.id.menuItemLang:
startActivity(new Intent("com.my.project.SETTINGS"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}catch(Exception e){
log(e);
}
}
First option
You have to override onOptionsItemSelected method in your Activity, which is called when user clicks on the item in Options menu. In the method you can check what item has been clicked.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item1:
Intent intent = new Intent(this, ActivityForItemOne.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
There is also onContextItemSelected method which works similary, but for Context menu (I'm not sure, what menu you mean).
More information at http://developer.android.com/guide/topics/ui/menus.html
EDIT:
Second option
I think the first option is easier, but from your code I see, that you want to start activity as an action (because of String parameter in Intent constructor). To do this, you need to specify an action in your AndroidManifest.xml. So, if I would start activity ActivityForItemOne (from previous example) the <application> element in AndroidManifest.xml would look like this:
<application ...>
...
<activity android:label="Activity For First Item" android:name=".ActivityForItemOne">
<intent-filter>
<action android:name="my.app.ITEMONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
And the Intent will be:
Intent intent = new Intent("my.app.ITEMONE");
The my.app. is package of your application. It's not necessary to use your application package, but it's recommended for uniqueness of actions.
More information at:
Class Intent - Action and Category constants
Action element
Intents and Intent Filters
Hope this solve your problem.
More optimice:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
return true;
case R.id.item2:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
if There have 2 Class
1 MainActivity
2 Welcome
then you need to go from
welcom>MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.logout:
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
break;
case R.id.settings:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}

Categories

Resources