I'm creating an App, with some classmates, and we want to share our Activities with others, so we don't have to do all over again. Is that possible?
You can copy the activity class and the XML layout of the activity from one project to another.
The activity class file goes in the source folder and the xml layout goes in the layouts folder.
To declare the activity in the manifest.xml you have to add:
<activity
android:name="com.example.stockquote.StockInfoActivity"
android:label="#string/app_name" />
with the correct name and label (You have to declare in manifest, otherwise it won't work).
You can try to open other activities from main activity with:
Intent myIntent = new Intent(this, MyActivityName.class);
startActivity(myIntent);
In that case, you can add a Button to open the new activities and test if it works:
Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(this, MyActivityName.class);
startActivity(myIntent);
}
});
You can copy the activity class into the Java folder in your project and the XML layout of the activity from one project to another, paste it in the layout folder (under "res").
Remember to fix the package name, and define the activity in the manifest! <activity android:name=".the.right.path.ActivityName"/>
you should enter a path according to the package structure in your project
good luck
Related
I have 5 different activities in my app, i enable the user to navigate between those activities by clicking buttons. The main activity has a RecyclerView in it (used to make a feed). the other activities are used for search, profile, notifications, etc. The user opens the app, the main activity is displayed. The user can scroll through the feed (which is the RecyclerView) in the main activity, now the user decides to go to other activities, search, profile, etc. Now, when the user clicks on the button that navigates to the main activity, I want to open back the main activity and display the recycler view at the same scrolling position and with the same items as was when the user just left it off.
Note: I'm not talking here about pressing the back button or using any goBack functions. I want to enable the user to use the main activity and visit all other activities and after all of that he will be able to come back to the main activity and find the feed exactly as he left it off.
In every activity I have five navigation buttons that allowing the user to go from every activity to every activity: click on profileBtn will open the profile activity, etc.
There is a way in which i can back up the state of the recycler view in the main activity before the user wants to go to other activities and restore it when the user clicks on the homeBtn?
Code:
HomeActivity.java:
public void profileBtn_OnClick(android.view.View view)
{
Intent intent = new Intent(getBaseContext(), ProfileActivity.class);
startActivity(intent);
}
ProfileActivity.java, SerachActivity.java, NotificationsActivity.java:
public void homeBtn_OnClick(android.view.View view)
{
/* HERE I WANT TO OPEN BACK THE HOME ACTIVITY AND DISPLAY THE FEED
EXACTLY AS IT WAS WHEN THE USER LEFT IT, HOW DO I DO THAT? */
Intent intent = new Intent(getBaseContext(), HomeActivity.class);
startActivity(intent);
}
You can use the launchMode="singleTask".
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to the existing instance through
a call to its onNewIntent() method, rather than creating a new one.
You can get more information about launchMode here: https://developer.android.com/guide/topics/manifest/activity-element
android:launchMode=["standard" | "singleTop" | "singleTask" | "singleInstance"]
<activity
android:name=".ui.HomeActivity"
android:launchMode="singleTask" />
Please do NOT use the special launch mode singleTask for this purpose. It causes more problems than it solves. These launch modes have complex side-effects that are not obvious and will cause you more problems later.
Your problem is simple to solve using standard Android behaviour. Change your method to look like this:
public void homeBtn_OnClick(android.view.View view)
{
Intent intent = new Intent(getBaseContext(), HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
The flag CLEAR_TOP tells Android to remove all activities from the task that are on top of the target Activity (in this case the target Activity is HomeActivity.
The flag SINGLE_TOP tells Android not to recreate HomeActivity, but to reuse the existing instance of it.
How can I add shortcut of my application to Android homescreen before I lauch app?
I need it added right after installation of app.
if you publish your app in google play store after installing app auto-create shortcut but if you want to handle that Android provides us an intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to the home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut.
First, we need to add permission INSTALL_SHORTCUT to android manifest XML.
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
The addShortcut() method creates a new shortcut on the Home screen.
private void addShortcut() {
//Adding shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);
}
Note how we create shortcut Intent object which holds our target activity. This intent object is added into another intent as EXTRA_SHORTCUT_INTENT.
Finally, we broadcast the new intent. This adds a shortcut with the name mentioned as EXTRA_SHORTCUT_NAME and icon defined by EXTRA_SHORTCUT_ICON_RESOURCE.
Also put this code to avoid multiple shortcuts :
if(!getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).getBoolean(Utils.IS_ICON_CREATED, false)){
addShortcut();
getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).edit().putBoolean(Utils.IS_ICON_CREATED, true);
}
I want to create an intent in MainActivity that will launch CompassActivity.
Both classes share a common layout activity_main, but use different parts of it.
Here is the intent I currently have in MainActivity which is supposed to open the CompassActivity.java class file but doesn't.
public void startCompass(View v)
{
Intent intent = new Intent(this, CompassActivity.class);
startActivity(intent);
}
What I have tried:
Altering androidmainfest.xml.
Changing the intent itself.
Changing the SDK version.
All you have to do is place the correct variables under the onCreate method and it should work fine.
Do you want to open an intent or do you just want to show/hide the different views you are using?
Instantiate your Views (findViewById) in your onCreate, then you can call view.setVisibility(View.GONE or View.VISIBILE) to hide or display your different views.
Bottom line, your views exist already on the main Activity (so no Intent needed to start another Activity) - you just need to control which views are visible at a particular time.
I have just opened my code from a few weeks off and now it doesn't seem to work. I have a splash screen on open, which then moves to the home screen. The splash screen works, but when trying to move to the home page it falls over saying No Class Def Found. What does this mean and how can i fix it?
It says the problem is found with the below code which is in the splash screen class:
public void run()
{
//Finish the splash activity so it can't be returned to.
SplashScreen.this.finish();
// Create an Intent that will start the main activity.
Intent mainIntent = new Intent(SplashScreen.this, HomeScreen.class);
SplashScreen.this.startActivity(mainIntent);
}
please place HomeScreen in manifest file
<activity android:name=".HomeScreen"
android:label="#string/app_name">
</activity>
So what happened was I updated the Android SDK and apparently there have been a few problems with this, one being changing lib to libs. What I did was removed the activity and re-added. Thanks arcastro.
So if anyone has this problem from 16->17, check the activity's in the manifest file, and the lib folder
I am learning how to develop under Android. I've made a new project, main activity, and I wanted to design a new window. I've genereted the new Activty as it it described here
Best way to add Activity to an Android project in Eclipse?
But i can't get the visual editor for that new activity. I know that I suppose to create new layout but how to do it and connect it with that second Activity?
How to properly go back from the secondActivity(close it? minimize it? how?) to the mainActivity and don't loose information gathered why we were using secondActivity(for example what options user has made?
This is how i invoke the second Acitivity and it works fine.
Intent intent = new Intent(this,DrugieOkno.class);
startActivity(intent);
For question 1:
Here is a basic tutorial on how to create a new Activity. For a more comprehensive one containing more information about Android development you can see here.
For question 2:
For tansfering data between Activities here is a nice tutorial.
Hope that helps.
To add a new activity, follow the methods answered in this question. This way you will create a new activity without adding it to the Manifest manually. [every activity needs to be listed in the AndroidManifest.xml].
Say you create a new activity names Activity2.java. To add the new layout to the new activity, add a new xml file to res/layout folder, say activity2.xml [where you define your new activity's layout]
To link the new layout to the new activity, include this line in your newly created Activity2.java
setContentView(R.layout.activity2);
So it will look like this:
public class Activity2 extends Activity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
}
}
2 . Now if you want to send some data from your Activity1.java to Activity2.java, you need to use Bundles.
So, if you want to send say a String from Activity1, do the following in Activity1.java:
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent
To recieve data in your Activity2.java do the following (say, onCreate())
Bundle params = this.getIntent().getExtras(); //gets the data from the Intent
String firstName = params.getString("fname"); //gets value of fname