When I make a new Android Application Project
I see the automatic generate MainActivity.java file.
Why was the "OnCreate" protected
I remember this was private. I don't understand. why made this?
For example this:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
It never was private, if it was private then how you were able to Override it?? You might have seen it public because subclass can not reduce the visibility of a method. Read this question for clarification. Why can't you reduce the visibility of a method in a Java subclass?
Related
Well, I'm trying to make an app that needs to have some sort of ActionBar, which only have to schedule and design once, and then serve me for all my APP activity.
I'm not sure how this works, need a little guidance please.
To give you an idea of what I want, I would like something like this:
A top, that is the general of the APP. And then below as well as a submenu with icons, with different options.
What exactly would have to use it? I've been tinkering with ActionBar but that it me for the top .. But to the other as you would?
Both want them to be static and are always in every activity of my APP, without having to copy and paste the code of each una..Que I guess and this somehow you are ready to embed it anywhere, programming only once.
This question doesn't really have much to do with Android. This is about how you can re-use code multiple places.
You could use inheritance for this purpose.
Say you have 3 different activities, but you want them to have a set of common features - in your case the ActionBar.
In that case you could create an abstract class that implements the ActionBar and make all your activities inherit from this abstract class.
The hierarchy could look something like this:
public abstract class BaseActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.oncreate(savedInstanceState);
// Setup your common ActionBar here.
}
}
Now for this abstract class to do its work, you'll have to make all your activities inherit from this, like so:
public class MyActivityA extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.oncreate(savedInstanceState); // It's important to call through to super to have it setup the ActionBar for the current activity.
// Next call setContentView(R.layout.my_activity_layout);
// And what else you need to do.
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_a_menu, menu);
return true;
}
}
And for the next Activity you do the same:
public class MyActivityB extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.oncreate(savedInstanceState);
// Next call setContentView(R.layout.my_activity_layout);
// And what else you need to do.
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_b_menu, menu);
return true;
}
}
The onCreateOptionsMenu is implemented in all your activities, as to create different menu items and the same should go for your onOptionsItemSelected.
Now this is a very basic example of how to share basic features for multiple classes and it should be something you'd have to be knowledgable about before you start working with Android, as the above code is common Java.
Also keep in mind, that it's a very broad question you're asking.
I actually just wanted to put this in a comment, but decided it would become too large to fit into the comments.
When I create a new activity All the code is generated. But R.java class is not updated.
Please can anyone come to my rescue. I am about to finish this project. Its the error near Activity_maininfo.
public class Maininfo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maininfo);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.maininfo, menu);
return true;
}
}
One of the following should work
Project -> Clean,
Right click -> Fix Project Properties
Check all of your XML
Maybe try restarting Eclipse
so, in my main activity, on the onCreate() method, I check if it is the app first run with shared preferences... If it is the first run of the app, the user is redirected to a welcome activity, and then, when I press the back button and return to the main activity, the title in the action doesn't show up.. I have tested with api 9 and 17, and this only happens with api 9, so I'm guessing the error must be something about using the support library for the action bar .. Can someone help me ?
Main:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
public class MainActivity extends ActionBarActivity {
SessionManager session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// session
session = new SessionManager(getApplicationContext());
// check first time app run
session.checkFirstRun();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
SessionManager First Run check method
// check first run
public void checkFirstRun() {
if(getFirstRunStatus() == true) {
// set first run key as false
editor.putBoolean("FIRST_RUN", false);
editor.commit();
// first time running the app, redirect user to welcome activity
Intent i = new Intent(_context, WelcomeActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
}
public boolean getFirstRunStatus() {
return pref.getBoolean(FIRST_RUN, true);
}
Add the title in a couple ways:
XML:
<activity
android:name=".....WelcomeActivity"
android:icon="#drawable/logo"
android:label="#string/app_name"
</activity>
On the Fly:
.setTitle("TITLE");
.setIcon(R.drawable.logo);
You can pass the title in an intent to if you want it to be dynamic... not sure if that is what you want:
.setTitle(extras.getString("title"));
Hope that helps.
try this in your onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_menu_actions, menu);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("title");
actionBar .setDisplayShowTitleEnabled(true);
// OR:
// getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
return true;
}
I have an app with 5 activities. All these activities have settings that can be modified using the menu-button (optionsmenu) and selecting 'Settings'. This will open a dialog where all settings shown and where modification is possible.
When I close this settings-dialog by press the 'ok'-button, I want the activity that called optionsmenu to update its view.
The optionsmenu is activited like this in all activities:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.firstactivity_options_menu, menu);
return true;
}
And an example of onOptionsItemSelected follows...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.settings) {
class.settingsDialog(this);
} else if (item.getItemId() == R.id.about) {
try {
alertDialog(getResources().getString(R.string.settings_about), getAssets().open("about"), this);
} catch (IOException e) {
}
}
return true;
}
My problem is updating an activities view after I've had a match for R.id.settings. Is this possible? And if so, please give me some pointers...
There's probably different ways to do this but after clicking Ok you can use an Intent to start the activity and call finish() on the activity to re-create the views
I would implement a super/subclass approach or an interface since Dialogs do not force an Activity's onResume() method to be called.
Make a top abstract Activity class, e.g. SuperMainActivity.
public abstract class SuperMainActivity extends Activity
{
public abstract void updateUI();
}
Then, have each of your Activities extend SuperMainActivity instead of just Activity and implement the updateUI() method.
Then in your settingsDialog() method, make it either:
Accept a SuperMainActivity param instead of an Activity/Context param.
or:
Do a cast when you want to callback, such as
((SuperMainActivity)myVariable).updateUI();
An interface is largely similar:
public interface ActivityCallback
{
public void updateUI();
}
And each activity will implement ActivityCallback
such as
public class MainActivity implements ActivityCallback
{
public void updateUI()
{
//implementation. Differs per class
}
}
Then again, your settingsDialog() method should accept in an ActivityCallback parameter or you will cast again.
Note that if you do decide on this approach, when you call settingsDialog() you can still call it with
settingsDialog(this);
Since your Activities will meet the requirements for a parameter.
I've written an application that creates a map activity. From there the user can switch to a menu and goes back to the map activity. After about 10 of those loops, the following error occurs:
02-28 21:35:54.780: E/AndroidRuntime(23502): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
I've tried the unbind drawables solution proposed here http://www.alonsoruibal.com/bitmap-size-exceeds-vm-budget/ and in various other threads but that did not help.
The only thing that helps is closing the map activity manually via finish(), but that causes an unnatural navigation behavior.
Here is my code:
MapActivity class
public class TestMapsForgeActivity extends MapActivity {
View mapView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapView = new MapView(this);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map_menu, menu);
return true;
};
#Override
public boolean onOptionsItemSelected(MenuItem item) {
startActivity(new Intent(getApplicationContext(), MenuActivity.class));
return true;
}
}
MenuActivity Class
public class MenuActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
};
#Override
public boolean onOptionsItemSelected(MenuItem item) {
startActivity(new Intent(getApplicationContext(), TestMapsForgeActivity.class));
return true;
}
}
What I don't understand is, the garbage collector does apparently not destroy the MapActivity properly unless I close it with finish(). But shouldn't android call finish() by itself as soon as the application needs more memory?
Does anyone have some thoughts on this issue?
Thanks in advance!
I think the problem is that you are starting an activity over another that wasn't closed.
Try this:
Intent i = new Intent(getApplicationContext(), TestMapsForgeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Setting the Intent flag CLEAR_TOP, will finish the others previous activitys, read more here: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP