I try to to add an Splash Activity to my application
this splash activity open when the program start for first time
but when the app work and skip the splash activity to main activity
and user hold on and go to the home screen
after that the user will return to the app ,,
the problem is here
so the application reopen the splash activity again
i don't want the app to open the splash activity every time the application resume activity
i need to direct open the main activity ...
I tried this code but it doesn't work with me
In Splash Activity ...
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Class<?> activityClass;
try
{
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
activityClass = Class.forName(prefs.getString("lastActivity", MainActivity.class.getName()));
}
catch(ClassNotFoundException ex)
{
activityClass = MainActivity.class;
}
startActivity(new Intent(this, activityClass));
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
}
);
}
}
and in the Main Activity i use this code
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
}
}
Use Application class to save a global flag:
public class App extends Application {
private static boolean sFirstRun = false;
public static boolean fetchFirstRun() {
boolean old = sFirstRun;
sFirstRun = false;
return old;
}
//--called when app process is created--
#Override
public void onCreate() {
super.onCreate();
sFirstRun = true;
}
}
And in manifest.xml add : <application android:name=".App"
now, App.fetchFirstRun() is true only when App process is created from scratch. As long as app is running subsequent calls will return false.
Related
I try to run LogIn activity from MainActivity but if I dont add FINISH() after
startActivityForResult, intent wont start
on MainActivity
**pref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean mojpin = pref.getBoolean("PinLock",false);
if(mojpin && loged==false){
Intent i = new Intent(MainActivity.this,AppPinLock.class);
MainActivity.this.startActivityForResult(i,1);
finish();
}**
On LogIn Activity
**String mojpin = getPassword();
if(mojpin!= ""){
String mojpin2 = pass.getText().toString();
if(mojpin.equals(mojpin2)){
naslov.setText("Pin is ok");
Intent i = new Intent(AppPinLock.this,MainActivity.class);
setResult(1, i);
startActivity(i); //if I dont add this wont back to MainActivity
finish();**
Yeah, You shouldn't close MainActivity after calling startActivity();
Explanation: If you start an Activity in MainActivity, the Method
#Override
protected void onPause() {
super.onPause();
}
Will be called. But if you call finish(); after calling the other Activity in the MainActivity, Android instantly Terminates the MainActivity completly and you can't get back until you call the MainActivity again. Example Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this, Activity2.class));
//DO NOT use finish(); here!!! (It will kill this Activity after launching Actiivty2)
}
}
Activity2
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
finish(); //this will close this Activity and you will get back to the last Activity. (In this case MainActivity)
}
}
I have the MainActivity and the secon_Activity in Android Studio, from MainActivity I go to second_Activity using Intent, then in the second_Activity I want to finish that activity, I do it but the first activity remains in background, how can I do to end or kill both activities.
Here the MainActivity:
public class MainActivity extends AppCompatActivity {
Button send_button;
EditText send_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_activity);
send_button = (Button)findViewById(R.id.send_button_id);
send_text = (EditText)findViewById(R.id.send_text_id);
send_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
String str = send_text.getText().toString();
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
intent.putExtra("message_key", str);
startActivity(intent);
finish();
}
});
}
}
Here de second_Activity:
public class Second_activity extends AppCompatActivity {
TextView receiver_msg;
Button finish_button;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_activity);
receiver_msg = (TextView)findViewById(R.id.received_value_id);
finish_button = (Button)findViewById(R.id.finish_button_id);
Intent intent = getIntent();
String str = intent.getStringExtra("message_key");
receiver_msg.setText(str);
finish_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
finish();
}
});
}
}
I've built a Kiosk app and from my searching you can't remove it from the "recent" apps.
You can how ever, use ActivityManager as follows:
ActivityManager am = (ActivityManager) Context.getSystemService(ACTIVITY_SERVICE);
am.moveTaskToFront(myAppId, 0);
am.killBackgroundProcesses(Activity_A_Package_Name);
Requires:
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
It should kill it, usually.
you can use System.exit(1) instead of finish()
Im working on offline login based application using SQLite. For the purpose of security i need to move to login activity if the app has minimized. i tried this code in onPause function
#Override
protected void onPause() {
Intent intent = new Intent(dashboard.this, login.class);
startActivity(intent);
finish();
super.onPause();
}
when i try to move from that activity to another also it moves to the login activity. i hope when im moving to another activity current activity is set to paused. that's why it moves to login activity.
you must set a Boolean to handle loginActivity ;
boolean mustGoToLoginActivity=true;
when you want to go to another Activity first set this boolean to false for example :
public void goToAnotherActivity(){
mustGoToLoginActivity=false;
//...
startActivity(anotherActivity);
}
and in onResume() methode set taht to true .
then in onPause() method :
#Override
protected void onPause() {
if(mustGoToLoginActivity){
Intent intent = new Intent(dashboard.this, login.class);
startActivity(intent);
finish();
}
super.onPause();
}
You are right, this scenario will not work. What you can do is use a static boolean to achieve the behavior like this.
public class MainActivity extends AppCompatActivity {
static Boolean navigateToLogin =false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (navigateToLogin) {
move();
navigateToLogin =false;
}
}
private void move() {
Intent intent = new Intent(this, newActivity.class);
startActivity(intent);
}
#Override
protected void onPause() {
super.onPause();
navigateToLogin = true;
}
}
I'm new to Android and I'm stuck on a conception problem.
I have an application with several activities, some of those activities are critical and require the user to be logged in a webApp.
When the user clicks a button to reach one of those critical activities, I call a background Service which ask if the user is still connected to the webApp (not timeout). If the user is logged in, the activity is started, otherwise a dialog pops up and ask for username and password. The problem is that there is several protected activities, and I want to use the same service to do the verification. The way I do it for the moment works but it's kind of kludgy.
public class A_Activity extends Activity {
Context context;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
setButtonClickListener();
}
private void setButtonClickListener() {
button_1 = (Button)findViewById(R.id.button1);
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intentCall = new Intent(context,com.them.cp.ConnexionManagerService.class);
intentCall.putExtra("WHO_IS_CALLED","FIRST_ACTIVITY");
context.startService(intentCall);
}
});
button_2 = (Button)findViewById(R.id.button2);
button_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intentCall = new Intent(context,com.them.cp.ConnexionManagerService.class);
intentCall.putExtra("WHO_IS_CALLED","SECOND_ACTIVITY");
context.startService(intentCall);
}
});
}
}
And my service
public class ConnexionManagerService extends Service{
public class IsConnectedAsync extends AsyncTask<String , Void, Void>{
protected Void doInBackground(String... whoIsCalled) {
String redirectedURL = getRedirectedURL();
if(redirectedURL.equalsIgnoreCase(IF_NOT_CONNECTED_URL)){
if(whoIsCalled[0].equalsIgnoreCase("FIRST_ACTIVITY")){
Intent trueIntent = new Intent(getBaseContext(), FirstActivity.class);
trueIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(trueIntent);
}
else if(whoIsCalled[0].equalsIgnoreCase("SECOND_ACTIVITY")){
Intent trueIntent = new Intent(getBaseContext(), SecondActivity.class);
trueIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(trueIntent);
}
}
else{
Intent falseIntent = new Intent(getBaseContext(), PopUpLoginActivity.class);
falseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(falseIntent);
}
}
return null;
}
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d("service onCreate", "onCreate");
}
public int onStartCommand(Intent intent, int flags, int startId){
String whoIsCalled = intent.getStringExtra("WHO_IS_CALLED");
new IsConnectedAsync().execute(whoIsCalled);
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
with my little knowledge I wish i could just send an intent, but it seems that it's not possible as it's not the UI thread.
My question is: What can I do to make this service more generic ?
I want to use the same service to do the verification.
If you don't destroy the service it will be the same service object. If an activity which started your service finishes or stops the service it could be destroyed if it was the unique activity that started the service. If you want to ensure that the service reminds on background start it on you application class (extending Application) and in each activity you need. When an activity stops the service or finishes the service will not be destroyed because your application class is still connected.
EDIT:
To avoid write putExtra again and again:
public class StartOrder1 extends Intent {
public StartOrder(Context ctx, String activity_name){
super(ctx, ServiceName.class);
if(activity_name != null)
super.putExtra("WHO", activity_name);
else
super.putExtra("WHO", "UNKNOWN");
}
public String getWho(){
reurn.getIntExtra("WHO");
}
}
To start it:
this.startService(new StartOrder1(this, "My activity name"));
The best solution:
public class StartOrder2 extends Intent {
public StartOrder(Activity a){
super(a, ServiceName.class);
super.putExtra("WHO", a.toString());
}
public String getWho(){
reurn.getIntExtra("WHO");
}
}
And you can override toString method in each Activity passing the activity name, class name, whatever you want. Then when you start an intent:
this.startService(new StartOrder2(this));
Or extends Activity with this utility:
public class EnhancedActivity extends Activity{
protected startMyService(String name){
Intent i = new Intent(this, MyService.class);
i.putExtra("who", name);
startService(i);
}
}
And call it on your final activity
[...]
super.startMyService("activity_name");
[...]
I am re-launching my application after my it crashes using Thread.setDefaultUncauhtExceptionHandler(). I would like to pass an intent extra telling it that it just came back from the dead, however its not sticking. Here is the onCreate of LockedUpActivity.
public class LockedUpActivity extends Activity {
/** Called when the activity is first created. */
UncaughtExceptionHandler defaultHandler;
private static final String RECOVERED = "recovered";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setDefaultHandler();
if (getIntent().getBooleanExtra(RECOVERED, false)) {
Log.i("LockedUp", "Back from the dead!");
((TextView) findViewById(R.id.textview)).setText("Back from the dead!");
}
else {
Log.i("LockedUp", "Machiavelli in this..");
}
}
public void goDownInFlames(View v) {
startActivity(new Intent(this, GoingDownActivity.class));
}
private void setDefaultHandler() {
defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread thread, Throwable ex) {
Intent i = new Intent(getApplicationContext(), LockedUpActivity.class);
i.putExtra(RECOVERED, true);
startActivity(i);
defaultHandler.uncaughtException(thread, ex);
}
});
}
}
As you can see, I am setting the extra, however it is never "Back from the dead!"
When an Application crashes or is 'forced stop', automatic garbage collection is done, variables are cleared, and so is the activity stack of the app. The extras won't remain there if the app crashes.