Android: How to start an Activity via button? - java

Noob Android developer here I'm more graphics than code but thought I'd start doing more coding. Anyway, I have some buttons on my main activity page and I want it so when the button is clicked it opens another class/activity. I've tried all the methods I've looked up and something is still not working, when I click the button in the emulator it just doesn't do anything, does't forcestop or anything just nothing, someone point me in the right direction please.
Code from the main page where the button lives:
public class StartingPoint extends Activity {
protected void onCreate(Bundle aim) {
super.onCreate(aim);
setContentView(R.layout.main);
final Button bSL = (Button) findViewById(R.id.bSongList);
bSL.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent SongList = new Intent(StartingPoint.this, SongList.class);
StartingPoint.this.startActivity(SongList);
}
});
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myname.appname"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<activity
android:name=".Splash"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="myname.appname.SPLASH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingPoint"
android:label="#string/app_name" >
<intent-filter>
<action android:name="myname.appname.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SongList"
android:label="#string/app_name" >
<intent-filter>
<action android:name="myname.appname.SONGLIST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
-L
Last 3 lines of logcat just after clicking the button in question.
01-02 21:59:50.473: I/ActivityManager(75): Starting: Intent { cmp=myname.appname/.SongList } from pid 681
01-02 21:59:52.953: I/ActivityManager(75): Displayed myname.appname/.SongList: +2s351ms
01-02 21:59:58.565: D/dalvikvm(348): GC_EXPLICIT freed 8K, 55% free 2591K/5703K, external 1625K/2137K, paused 520ms

Try this:
Intent songList = new Intent(StartingPoint.this, SongList.class);
startActivity(songList);
Are you getting any errors? You need to add every Activity you create in the manifest. If you have two activities and only the Main in the manifest, that could be you problem.
On second thought, I believe your manifest is wrong. Check this. With the Main (Your starting point) and the Menu which is the second one:
<activity android:name="com.activities.Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.activities.Menu"></activity>
Try this manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myname.appname"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<activity
android:name=".Splash"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingPoint"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SongList"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
You don't need to add the filter on each activity. I understood that you have only one entry Activity and that is the splash screen. However, you might need to change the manifest to change the ".Splash" and every other Activity to the full path including the package.

// whats your package name for your SongList
as your log cats shows
01-02 21:59:50.473: I/ActivityManager(75): Starting: Intent { cmp=myname.appname/.SongList } from pid 681
01-02 21:59:52.953: I/ActivityManager(75): Displayed myname.appname/.SongList: +2s351ms
//use full package name in your activity in manifest.xml
<activity
android:name="myname.appname.SongList"
android:label="#string/app_name" >
</activity>

Your activity does not start because it has a NullPointerException in it. (Line 10). When you do a findViewById on something that is not in your layout, most likely.
When facing this kind of problems, reading the red lines is usually helpful. It basically says: "Hey! you have a null object in SongList line 10 when you try to start it!"
Edit
ImageView ivlogo = (ImageView) findViewById(R.id.ivsonglogo);
ExpandableListView elv1 = (ExpandableListView) findViewById(R.id.elv1);
This can never work. This is done during the object initialization, much before onCreate is called, therefore much before you have called setContentView.
You must initialize your widgets after having set the view.

Intent SongList = new Intent(StartingPoint.this, SongList.class);
startActivity(SongList);
and in Manifest type this
<application .....>
<activity android:name=".CustomSurfaceView"></activity>
</application>

For the code try this:
Intent songListIntent = new Intent(this, SongList.class);
StartingPoint.this.startActivity(songListIntent);
And for manifest:
<activity android:name=".SongList"></activity>

Related

Unable to remove activity from stack,

I am starting an activity from service. ,
In this i set two flags one is
Intent.FLAG_ACTIVITY_NEW_TASK
because i am running activity from service.its new
Intent.FLAG_ACTIVITY_NO_HISTORY is set to new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished.
Here is the code which i call from service to open activity
public static void open(Context context){
Intent intent=new Intent(context,PopupActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(intent);
}
and i have cross button in my activity to destroy activity , when i press cross button activity should remove from stack,but it remain in stack and when press home button to view it, activity recreated in stack.for example "oncreate" is again called
here is my code.
#OnClick(R.id.flClose) void closeWindow(){
finish();
}
Here is my manifest activity code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackagename">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:name=".app.MyApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DashboardActivity"
android:screenOrientation="portrait" />
<activity
android:name=".SearchUserActivity"
android:screenOrientation="portrait" />
<activity
android:name=".TermsPolicyActivity"
android:screenOrientation="portrait" />
<activity
android:name=".UserDetailActivity"
android:screenOrientation="portrait" />
<activity
android:name=".DocumentViewerActivity"
android:screenOrientation="portrait" />
<activity
android:noHistory="true"
android:name=".PopupEndActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="#style/Theme.AppCompat.Transparent.NoActionBar" />
<activity
android:name=".BlockActivity"
android:theme="#style/Theme.AppCompat.Transparent.NoActionBar" />
<activity
android:name=".BlockSettingsActivity"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation|keyboardHidden"
/>
<service
android:name=".service.SyncDataService"
android:enabled="true"
android:exported="false" />
</application>
</manifest>
Try using finishAffinity() instead of finish()
Add the following flags instead of setflags to open activity
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
And on close button call the function below
private void endTask() {
if (Build.VERSION.SDK_INT >= 21) {
finishAndRemoveTask();
} else {
finish();
}
}
You can get this behaviour by setting the android:noHistory attribute to "true" in the relevant entries in your AndroidManifest.xml file.
For example:
<activity
android:name=".YourActivity"
android:noHistory="true" />
I solved My problem by Adding flag Intent.FLAG_ACTIVITY_NEW_DOCUMENT This flag is used to open a document into a new task rooted at the activity launched
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
In manifest i have noHistory
android:noHistory="true"

How to change Activity Priority in Android Studio

Iam trying to add Splash screen to my existing Android Project, which is not MainActivity.Java, but by default Android Studio is executing MainActivity.Java first, So I want to change the Activity Priority so that my SplashActivity.Java will execute first and later Activity's will follow after the Splash Screen.
Make splash activity your launcher activity instead of Main Activity in Android Manifest.
<activity
android:name=".ui.splash.SplashActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
modify your AndroidManifest.xml like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.circlefil.reactivetrip">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"/>
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
You need to add IntentFilter in you splash screen activity and from splash screen activity you have to link your main activity using Intent.
Add following code to your splash screen activity.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
How ever keeping an entire activity is not the best way to create splash screen. See this for more info: Here
To Start activity first you need to make it Launcher Activity in AndroidManifest.xml file.
<activity
android:name=".StartingActivityName">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

startActivity crashes if I do not open the activity from another path

So I have 2 ways an activity can be opened. One is from the activity flow of:
Main > Tracks > Day > Topic > TrackSelect > TrackInfo
and the other is:
Main > MySchedule > TrackInfo
If I try to get TrackInfo to open up via the second path, it crashes the app.
However, If I go from the first path, then all the way back to the main, then through the second path, it works perfectly. Is there something weird going on?
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fayko.conference_app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Conference-App"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".mainSelection"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".topicScreen"/>
<activity
android:name=".myScheduleScreen" />
<activity android:name=".trackSelection" />
<activity android:name=".mainScreen" />
<activity android:name=".daySelection" />
<activity android:name=".trackInfoScreen" />
<activity android:name=".mapChoose" />
<activity android:name=".sponsorScreen" />
<activity android:name=".committeeScreen" />
<activity android:name=".welcomeScreen"></activity>
</application>
</manifest>
Code from TrackSelect > Track Info:
Intent intent = new Intent(trackSelection.this,trackInfoScreen.class);
startActivity(intent);
Code from MySchedule > TrackInfo:
Intent intent = new Intent(myScheduleScreen.this,trackInfoScreen.class);
startActivity(intent);
I appreciate any help you guys can give me.
It turns out that the issue I was having was due to some internal code. Even thought he stacktrace was saying things about socket lost for the debugger.

How to change first activity in Android Studio?

I want to change my first activity, first I have activity named MainActivity. And I add new activity named ActivityFirst. Surely I have to change AndroidManifest.xml. Previously I have androidManifest like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edmt.dev.androidsimsimi">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And after I add my new activity, also I change my androidManifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edmt.dev.androidsimsimi">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".ActivityFirst">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I'm confused where I put the MainActivity after I add ActivityFirst. This my ActivityFirst.java:
public class ActivityFirst extends AppCompatActivity {
private Button mulai;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
mulai = (Button) findViewById(R.id.mulai);
mulai.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityFirst.this, MainActivity.class);
startActivity(intent);
}
});
}
}
You need <activity> tags for each Activity in your app's manifest file as mentioned here in the docs:
All activities must be represented by elements in the
manifest file. Any that are not declared there will not be seen by the
system and will never be run.
So in your case, it'd be like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edmt.dev.androidsimsimi">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".ActivityFirst">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
In Android, The first Activity to show when a app is launched is tagged LAUNCHER in your manifest file.
All you need do to change the Launcher Activity is to add an intent-filter tag inside the activity tag.
So if your activity tag was self closing, you change it and add intent filter tag inside the tag.
Here is an illustration.
<activity android:name=".MainActivity" />
<activity android:name=".ActivityFirst">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The above code in your Manifest will make ActivityFirst the launcher activity,
while the below code will make MainActivity the launcher activity.
<activity android:name=".ActivityFirst" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
I hope this helps.

android unable to start Main activity in options menu

Hi there basically i have created a options menu and have one of the menu items as the home button which essentially calls the home activity Main.java. I have used the code
startActivity(new Intent("org.me.myandroidstuff.Main"));
in a GetHome method. My problem is that when i run this the application crashes because it isnt handled. Now i know this generally means there is some error in the androidmanifest file however i think my coding is okay so im a bit stumped. Here is my androidmanifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.me.myandroidstuff"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" android:allowBackup="false">
**<activity
android:name=".Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>**
<activity
android:name=".PetrolPriceActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="org.me.myandroidstuff.PetrolPriceActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".AreaURL"
android:label="#string/app_name" >
<intent-filter>
<action android:name="org.me.myandroidstuff.AreaURL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
the part highlighted is the code for my main class. Im rather new to android so i imagine its a relatively simple fix but ive looked and i havent found a solution yet. Thanks
change the code to
startActivity(new Intent("android.intent.action.MAIN"));
In manifest file in action you have written
< action android:name="android.intent.action.MAIN" />
while calling the new Intent(String Action), you need to put the same action name.
or you can use different signature of constructor,
change the code to
startActivity(new Intent(this, Main.class);
in place of "this" you can use the appropriate context.
use startActivity(new Intent(getApplicationContext(), Main.class));

Categories