Access menu class from MainActivity class in android - android

I have two different classes. My main activity class and a separate menu.class. I want to be able to get the menu class to load from the mainactivity.class. The menu button does not work on my device or emulator when the mainActivity class is running. I am not sure how to call the menu.class from the mainActivity. I do not want to put the menu java code in the mainActivity.
I am asking for the way to add a snip of code to load a menu activity from one class to another, not to fix my code. I can not find any examples online to show how to load a menu class that is not also written in your main activity class.
public class lifeMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.life_menu);
}
That is a snip of my menu activity. My main activity is called MainActivity

You can make your MainActivity extend your MenuActvity that entends Activity. Hard to know since you did not provide a snip of your menu class.

Related

How to reuse a same Navigation Drawer in different activities?

My drawer is created by android studio automatically and I realized that its structure is not as it was like when I created handly. API23 suggests developers to use assembled layout files. How can I use the same drawer in different activities with API23?
create xml of only drawer component and include it in every activity layout.
After that to write java code of drawer create CommonActivity class which extends activity and write here drawer now user CommonActivity rather than extending Activity class in every activity class
like
public class CommonActivity extends Activity
{
oncreate
{
//drawer code
}
}
public class YourActivity extends CommonActivity
{
//No need to write drawer code here again
}

Access an Activity from a Fragment plugged into another Activity

I have been using this example as a base for my project.
I have changed the project to use an ArrayAdapter for the titles-fragment's ListItems and changed the DetailsFragment's View to display a custom layout, which as a Button that is supposed to add an entry into a database.
Instead of generating widgets like in the example, I just inflate a custom XML into the FrameLayout besides the TitlesFragment in the 'layout-land' version of the layout.
My problem stems from the fact that DetailsFragment is plugged into the MainActivity when in Landscape mode but gets it's own DetailsActivty if it is in Portrait mode.
The Button I have in my custom layout for the details-fragment calls a function in its onClick() that is called AddNewItem.
So when the Button was clicked in Landscape mode, it crashed, because there was no AddNewItem in the MainActivity. I solved this by using a BroadcastReceiver, so when the button is clicked, a method named AddNewItem in MainActivity.java instead broadcasts a custom event and i have a BroadcastReceiver that calls the AddNewItem in DetailsFragment.java.
It looks like this:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
DetailsActivity m = (DetailsActivity) DetailsActivity.getActivityInstance(); // see below for what getActivityInstance does
LayoutInflater mInf = LayoutInflater.from(context);
View myView = mInf.inflate(R.layout.customlayout, null);
((DetailsActivity) m).AddNewItem(myView);
}
}
But this gives me:
08-11 13:37:50.687: E/AndroidRuntime(6766): java.lang.RuntimeException: Unable to start receiver in.falkeninc.umt_v0_9_8_1.MyReceiver: java.lang.NullPointerException
I am not sure what is happening. Because I am also using a static variable in DetailsActivity.java to be able to reach it inside the BroadcastReceiver. The code looks like this:
public class DetailsActivity extends SherlockFragmentActivity {
...
public static SherlockFragmentActivity activityInstance;
...
activityInstance = this; // in the onCreate
...
public static SherlockFragmentActivity getActivityInstance(){
return activityInstance;
}
}
My problem stems from the fact that DetailsFragment is plugged into
the MainActivity when in Landscape mode but gets it's own
DetailsActivty if it is in Portrait mode.
This shouldn't be a problem. Although you have two activities you have the same DetailsFragment class in both of them. If a Button from that fragment is doing something then you should keep it at that fragment's level(that would be an ideal fragment, one that is self contained, a fragment that doesn't know or care where is put). If you do require the activity for the work in that method, you should add extra details.
Also, if the behavior is common to both activities you could make a base activity holding that method and let the two current activities inherit from that, so it will be available to the fragment no matter what.
public static SherlockFragmentActivity activityInstance;
Don't keep static references to activities, you risk leaking them.
Also, don't access activities from outside their own context. When an activity is not the one interacting with the user(it's onPause() has been called) that activity could as well be completely destroyed and trying to access it in another activity could bring you lots of problems.

How do I create common code for parts of Android activities?

In my application there are 14 activities. Out of that 9 activity contains custom title bar and tab pane. so here I need to write this common code at one place instead of redundant code in each activity that contain custom title bar and tab pane code (i.e layout and it's activity specific code)
What are the possible ways to do this?
The common way is:
Create a super class called, for instance, CommonActivity which extends Activity
Put the boilerplate code inside that class
Then make your activities extend CommonActivity instead of Activity:
Here a simple example:
public class CommonActivity extends Activity{
public void onCreate(Bundle b){
super.onCreate(b);
// code that is repeated
}
protected void moreRepeatitiveCode(){
}
}
And your current activities:
public class AnActivity extends CommonActivity{
public void onCreate(Bundle b){
super.onCreate(b);
// specific code
}
}
Hmm.. Common code doesn't always need to be in Activity class but just regular class. Than we could call those methods according to our needs referring to the common code class.
Am I right with this example?
Of course in case we need it like Activity, above proposal would work perfectly if we take care of Activity lifecycle and we don't forget to add it to manifest file.
In general Activities should just create UI, handle events occurrences and delegate business logic and/or other actions to the other components in our App.
Cheers

Going Home Screen of app in android?

I want to go to homePage of my application on clicking button where i was in inner page.
Can any one tell me how to do that ?
Thanking you ,
Srinivas
I suggest creating an Application class. In that class have a boolean field that is false by default. Every Activity should check if that field is true in onResume() and call finish() if it is true (except for the main activity that always sets the field to false in onResume()). You can even create a custom Activity that does this and then have all activities extend that activity.
Resources:
http://d.android.com/resources/faq/framework.html#3
The android.app.Application class
The android.app.Application is a base class for those who need to maintain global application state. It can be accessed via getApplication() from any Activity or Service. It has a couple of life-cycle methods and will be instantiated by Android automatically if your register it in AndroidManifest.xml.
Reference: http://d.android.com/reference/android/app/Application.html
Stripped-down example:
MyApp
public class MyApp extends Application { public boolean goBack = false; }
MyActivity
public class MyActivity extends Activity {
protected void onResume() {
if ( ((MyApp) getApplication()).goBack ) finish();
}
}
SomeActivity
public class SomeActivity extends MyActivity {
// nothing special here, it's all been implemented in MyActivity!
}
MainActivity
public class MainActivity extends Activity {
protected void onResume() {
((MyApp) getApplication()).goBack = false;
}
}
AndroidManifest.xml
[...] <application android:name=".MyApp" [...]> [...]
Note: You don't need to declare MyActivity in AndroidManifest.xml because it will never be launched directly (it will only be extended).
On your button click event add the following lines of code
moveTaskToBack(true);
There are probably two ways of doing that (general concepts):
The first one would be to simply launch the home actvity again, if you don't mind having it re-created again, unless that activity is a "singleTask" or "singleInstance" activity.
The second one would be to close the top activity in the stack as long as it is not your home activity. I don't see an easy way to achieve that, maybe by finishing the current activity with a specific result that gets checked by the launching activity, who will in turn close and send the result until the home activity is reached.

can we display multiple activities in a single activities?

I want to know if we can display multiple activities in a single activity using ActivityGroup.
Can anyone please help me out with this ?
Thanks in advance.
Regards,
Serenity.
extend it to your activitygroup class.
Class A extends ActivityGroup{}
Get the LocalActivityManager
Set the content view as shown below
this.setContentView(activity.getWindow().getDecorView());
Where activity -> reference of the activity which you would like to display.
Class B extends Activity{
//in onCreate()
Activity activity = this;
}
As seen in coderanch.com

Categories

Resources