18

I created an activity by accident (let's call it FooActivity) in Android Studio. What is the best way to delete this and all associated references/code? What I did was I deleted FooActivity.java, res/layout/activity_foo.xml, and the associated tag in AndroidManifest.xml. But I'm not sure if I'm missing anything else associated with FooActivity. Ideally, it would be nice to be able to do all of this automatically when I no longer need a given activity.

6 Answers 6

18

I'm answering my own question since I figured out a way to see exactly what got added after creating a new activity.

Since I was putting everything under Git version control, I realized that I could do a git diff with a fake activity to see exactly what the changes were. I discovered that the following files are added whenever a new activity is created:

FooActivity.java app/src/main/res/layout/activity_foo.xml app/src/main/res/menu/foo.xml 

In AndroidManifest.xml, the following is added:

<activity android:name=".FooActivity" android:label="@string/title_activity_foo" > </activity> 

In app/src/main/res/values/strings.xml, the following is added:

<string name="title_activity_foo">FooActivity</string> 

Of course, this wouldn't catch other references that were added after creating the new activity, so EyesClear's and Shahzad's answers would allow you to find these references. But in the future, I would probably checkout a new branch before creating a new activity so that I could nuke any changes that I don't like.

Sign up to request clarification or add additional context in comments.

Comments

5

I don't think automatic references removal is possible, but you can use the safe delete feature (right click on the activity -> delete -> check the safe delete option) to find all references in code and comments. Anyway, if a reference points to non-existing class, your project won't compile and an error will be shown with detailed information, so you'll be able to solve it quickly.

Comments

3

If you are on windows press Ctrl+Shift+F or Command+Shift+F for Mac to open the Find in Path dialog and enter FooActivity. This should show you all occurrences of FooActivity.

Comments

2

Just right click on activity which you want to delete -> Delete->Ok->Delete Anywayenter image description here

Comments

1

I was working on deleting an Activity and found a way.

You can right click on an activity->Find Usages(Alt+F7).

You will get a list of files where the activity is used in the project, and then delete.

Comments

0

To remove all reference and associated files of the FooActivity, I would press Control+Z just after the Activity creation,It'll then ask you to confirm the action. So that all the references and associated files would get deleted without any trouble.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.