Current Activity / Last Activity

Put your problem here if it does not fit any of the other categories.

Current Activity / Last Activity

Postby wildfandango » Wed Jun 02, 2010 11:42 am

Hi everyone!

I am new in this forum, newbie in android and semi-newbie in java :P

i have a little test proyect in eclipse with this structure:

MAIN
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package android.v1g0.pruebas;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.v1g0.aplicacion.App;
  6. import android.widget.Toast;
  7.  
  8. public class PruebasV1G0 extends Activity {
  9.     /** Called when the activity is first created. */
  10.        
  11.        
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.main);
  16.        
  17.         Activity a = App.activity();
  18.        
  19.         if (a == null){
  20.                 Toast.makeText(this, "NULL", Toast.LENGTH_SHORT).show();
  21.         }else{
  22.                 Toast.makeText(this, "NOT.NULL", Toast.LENGTH_SHORT).show();
  23.         }
  24.    }
  25. }
  26.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


LIB
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package android.v1g0.aplicacion;
  2.  
  3. import android.app.Activity;
  4. import android.app.LocalActivityManager;
  5. import android.content.res.Resources;
  6.  
  7. public class App {
  8.  
  9.         static LocalActivityManager activityManager;
  10.        
  11.         public static String name(Activity a){
  12.                 CharSequence appName = "";
  13.  
  14.                 Resources appR = a.getResources();
  15.                 appName = appR.getText(appR.getIdentifier("app_name","string",a.getPackageName()));
  16.                
  17.                 return (String) appName;
  18.         }
  19.        
  20.         public static Activity activity(){
  21.  
  22.                 Activity a = activityManager.getCurrentActivity();
  23.                
  24.                 return a;
  25.         }
  26. }
  27.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


but crashes at: Activity a = App.activity(); why :,( ?


the idea is obtain the current/last activity from a external class... for use in classes like...

LIB
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package android.v1g0.mensajes;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.app.ProgressDialog;
  6. import android.content.DialogInterface;
  7. import android.widget.EditText;
  8. import android.widget.Toast;
  9.  
  10. public class Msg {
  11.        
  12.     // TEST  
  13.     public static void dbug(String t)
  14.     {
  15.         a = App.activity();
  16.         Toast toast = Toast.makeText(a.getApplicationContext(), t, Toast.LENGTH_SHORT);
  17.         toast.show();
  18.     }
  19.  
  20. }
  21.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



thx for all...
wildfandango
Freshman
Freshman
 
Posts: 2
Joined: Wed Jun 02, 2010 11:14 am

Top

Re: Current Activity / Last Activity

Postby anarche » Thu Jun 03, 2010 12:58 am

Ok, why does it crash? Can you provide a log?

If you want to debug (and I realise that this is for practice but...) the ADK provides a Log static class for logging to the logcat.

If you don't know what the logcat is, search these forums.

If you want to practice - can I suggest going through the tutorials. Particularly the Notepad tutorial is excellent for understanding Activities.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Current Activity / Last Activity

Postby wildfandango » Thu Jun 03, 2010 6:10 pm

Hi anarche:

It crashes for this... (null ponter exception blablabla)

thx for te debugger sugestion, i must try the notepad project... i know

My problem is very simple...

how i can obtain the ACTUAL or LAST Activity from outside of the Activity.class???

inside of the Activity.class this.xxxx but outside how???

thx 4 all, sry for my bad english :P
wildfandango
Freshman
Freshman
 
Posts: 2
Joined: Wed Jun 02, 2010 11:14 am

Re: Current Activity / Last Activity

Postby anarche » Thu Jun 03, 2010 10:06 pm

AFAIK you cannot access the Activity stack - this would be a security issue.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 14 guests