Android applications mein activities ko link karna padta hai. ye linking within the application bi ho sakti hai and outside the application bi ho sakti hai .
Android mein hum applications ko different tarike se link kar sakte hain.
let suppose two activities hain:
Modifications in the manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.linking"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.linking" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
now main activity se activity2 ko link karne ke liye :
packagecom.example.linking;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---link to Activity2---
Intent i = new Intent("com.example.linking.Activity2");
startActivity(i);
}
}
kisi doosri activity se link karne ke liye hum Intent ka use karte hain. Hum intent ka object banate hain and then uske constructor mein hume jo activity manifest file mein define ki hai use pass kar dete hain.
After this hum activity ko start karte hain by using
startActivity(i);
alternatively hum isko ek aur method se kar sakte hain:
Intent i = new Intent();
i.setAction("net.learn2develop.Activity2");
startActivity(i);
yahan par hum intent ka object bana rhe hain and then setAction method ke through hum target activity ka name set kar rhe hain
Abhi jo humne code use kiya hai isse hum activity ko within the application as well as outside the application dono mein call kara sakte hain par agar hum chahte hain ki hum kewal within the application hi call karein
to hum activity ko direct class name se bhi call kar sakte hain.
//---link to Activity2---
Intent i = new Intent(this, Activity2.class);
iske saath hi hamein manifest file mein bhi modification karna padega
<activity
android:name=".Activity2"
android:label="@string/app_name" >
<intent-filter>
<!--
<action android:name=com.example.linking.Activity2" />
-->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
hamein intent filter se action element ko remove karna hoga.
Agar Hum jo activity call kar rhe hain and vo activity crash kar gyi hai tab hamein screen par aisa msg dekhne ko milega :
ise avoid karne ke liye hum startactivity ko hum intentCreator ke saath call karte hain jismein hum intent ka object pass karte hain and string pass karte hain jo ki display hogi application ke crash hone par .
Intent i = new Intent(this, Activity2.class);
startActivity(Intent.createChooser(i, "Choose an application"));
now the screen will be :
note: Agar createChooser() use kar rhe hain tab hum activity ko is tarah se define ni kar sakte
agar more than one activites indicate ho rhi hain then the screen will be :
Android mein hum applications ko different tarike se link kar sakte hain.
let suppose two activities hain:
Modifications in the manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.linking"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.linking" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
now main activity se activity2 ko link karne ke liye :
packagecom.example.linking;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---link to Activity2---
Intent i = new Intent("com.example.linking.Activity2");
startActivity(i);
}
}
kisi doosri activity se link karne ke liye hum Intent ka use karte hain. Hum intent ka object banate hain and then uske constructor mein hume jo activity manifest file mein define ki hai use pass kar dete hain.
After this hum activity ko start karte hain by using
startActivity(i);
alternatively hum isko ek aur method se kar sakte hain:
Intent i = new Intent();
i.setAction("net.learn2develop.Activity2");
startActivity(i);
yahan par hum intent ka object bana rhe hain and then setAction method ke through hum target activity ka name set kar rhe hain
Abhi jo humne code use kiya hai isse hum activity ko within the application as well as outside the application dono mein call kara sakte hain par agar hum chahte hain ki hum kewal within the application hi call karein
to hum activity ko direct class name se bhi call kar sakte hain.
//---link to Activity2---
Intent i = new Intent(this, Activity2.class);
iske saath hi hamein manifest file mein bhi modification karna padega
<activity
android:name=".Activity2"
android:label="@string/app_name" >
<intent-filter>
<!--
<action android:name=com.example.linking.Activity2" />
-->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
hamein intent filter se action element ko remove karna hoga.
Agar Hum jo activity call kar rhe hain and vo activity crash kar gyi hai tab hamein screen par aisa msg dekhne ko milega :
ise avoid karne ke liye hum startactivity ko hum intentCreator ke saath call karte hain jismein hum intent ka object pass karte hain and string pass karte hain jo ki display hogi application ke crash hone par .
Intent i = new Intent(this, Activity2.class);
startActivity(Intent.createChooser(i, "Choose an application"));
now the screen will be :
note: Agar createChooser() use kar rhe hain tab hum activity ko is tarah se define ni kar sakte
Intent i = new Intent(this, Activity2.class);
startActivity(Intent.createChooser(i, "Choose an
application"));
agar more than one activites indicate ho rhi hain then the screen will be :





0 comments:
Post a Comment