49

We all know how easily and safely to create a new Activity. Now I want to know how to remove an Activity from my project safely.

I've created some activity in my project and feel that they are now needless and I want to delete them. But if I delete manually these files:

  1. The activity's XML file in the /layout folder

  2. The activity's Java file in the /src folder

  3. The <activity></activity> portion from the manifest file

Then,

  • Will no other data related that activity stays in my project? What are those?
  • Is there any simple removal procedure like creating an activity in Eclipse?
A. K. M. Tariqul Islam
  • 2,824
  • 6
  • 31
  • 48

7 Answers7

58

I found my answer. To remove an activity, let the name of the activity is activity:

  1. Remove the activity.java file from src folder (java/your.package.name folder for AndroidStudio)
  2. Remove the activity.xml file from layouts folder (layout folder for AndroidStudio)
  3. Remove the activity.xml file from res/menu folder (this is an optional folder)
  4. Remove the <activity></activity> block of the activity from the manifest file
  5. If the <activity> block in the manifest file has a reference (usually in android:label tag) remove the referenced value (usually from values/strings.xml).

According to answers from @ojonugwa ochalifu and @Md Tarik Mahmud you should also right click on app folder then Refractor -> Remove Unused Resources

A. K. M. Tariqul Islam
  • 2,824
  • 6
  • 31
  • 48
24

Just do it in a two steps in Android Studio.

  1. Delete the Activity's Java file: YourActivity.java
  2. Select app from the Project-Browser in Left Panel. Click Refactor from menu, click Remove Unused Resources.

You are done.

Md Tarik Mahmud
  • 331
  • 3
  • 8
7

I don't think there is any procedure to delete an activity at once from everywhere.

Delete the entry from manifest first. Then delete the Activity class file. Once class file is deleted you will start getting errors where it is used. Look into each error separately and resolve them.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
4

And the title string in res/values/xml/strings.xml

Myat Min Soe
  • 787
  • 8
  • 32
  • 2
    This should really have been a comment to Rohit5k2's answer and not a separate answer, but thank you for your contribution. – Jamie Twells Aug 27 '14 at 12:04
3

Just to make sure you have deleted everything after following @CoolBrain's answer, do:

Refractor -> Remove Unused Resources

This will remove residues of that activity you aren't sure you've deleted. Note however that this operation removes unused drawables, and views also

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
2

Right click on each of the file associated with the activity (in Android View), select refactor, then select safe delete. This will also tell you if there are any use (of that file) which makes it not safe to delete.

tintin
  • 45
  • 5
0

I found my answer. To remove an activity, let the name of the activity is activity:

Remove the activity.java file from src folder (java/your.package.name folder for AndroidStudio) Remove the activity.xml file from layouts folder (layout folder for AndroidStudio) Remove the activity.xml file from res/menu folder (this is an optional folder) Remove the <activity></activity> block of the activity from the manifest file If the <activity> block in the manifest file has a reference (usually in android:label tag) remove the referenced value (usually from values/strings.xml). According to answers from @ojonugwa ochalifu and @Md Tarik Mahmud you should also right click on app folder then Refractor -> Remove Unused Resources

Thanks to @A. K. M. Tariqul Islam for answare.

user2738882
  • 1,161
  • 1
  • 20
  • 38